欢迎光临
我们一直在努力

vb.net和c#语法比较-.NET教程,VB.Net语言

建站超值云服务器,限时71元/月

由于一些人对vb.net和c#选择方面存在一些困惑,其实只是语法习惯问题,我把它们的语法列出来比较一下,大家有个感性认识。

1.变量声名 

c# 语法 

int x; 

string s; 

string s1, s2; 

object o; 

object obj = new object(); 

public string name; 

vb语法 

dim x as integer 

dim s as string 

dim s1, s2 as string 

dim o implicitly object 

dim obj as new object() 

public name as string 

2语句 

c#: 

response.write(“中文c#技术站”); 

vb: 

response.write(“中文c#技术站”) 

3.注释语句 

//中文c#技术站

/* 

欢迎访问

, 

中文c#技术站

*/ 

vb: 

中文c#技术站

4.获得url 传递的变量 

c#: 

string s = request.querystring[“name”]; 

string value = request.cookies[“key”]; 

vb: 

dim s, value as string 

s = request.querystring(“name”) 

value = request.cookies(“key”).value 

5.声明属性 

c#: 

public string name { 

get { 

… 

return …; 

set { 

… = value; 

vb: 

public property name as string 

get 

… 

return …; 

end get 

set 

… = value; 

end set 

end property 

6.数组 

c# 

string[] a = new string[3]; 

a[0] = “1”; 

a[1] = “2”; 

a[2] = “3”; 

//二维数组 

string[][] a = new string[3][3]; 

a[0][0] = “1”; 

a[1][0] = “2”; 

a[2][0] = “3”; 

vb: 

dim a(3) as string 

a(0) = “1” 

a(1) = “2” 

a(2) = “3” 

dim a(3,3) as string 

a(0,0) = “1” 

a(1,0) = “2” 

a(2,0) = “3” 

dim a() as string 

a(0,0) = “1” 

a(1,0) = “2” 

a(2,0) = “3” 

dim a(,) as string 

a(0,0) = “1” 

a(1,0) = “2” 

a(2,0) = “3” 

7变量初始化 

c#: 

string s = “hello world”; 

int i = 1 

double[] a = { 3.00, 4.00, 5.00 }; 

vb: 

dim s as string = “hello world” 

dim i as integer = 1 

dim a() as double = { 3.00, 4.00, 5.00 } 

8;判断语句(if 语句) 

if (request.querystring != null) { 

… 

vb: 

if not (request.querystring = null) 

… 

end if 

9.分支语句(case 语句) 

c#: 

switch (firstname) { 

case “john” : 

… 

break; 

case “paul” : 

… 

break; 

case “ringo” : 

… 

break; 

vb: 

select (firstname) 

case “john” : 

… 

case “paul” :… 

case “ringo” : 

… 

end select 

10 for循环语句 

c# 

for (int i=0; i<3; i++) 

a(i) = “test”; 

vb: 

dim i as integer 

for i = 0 to 2 

a(i) = “test” 

next 

11 while 循环 

c#: 

int i = 0; 

while (i<3) { 

console.writeline(i.tostring()); 

i += 1; 

vb: 

dim i as integer 

i = 0 

do while i < 3 

console.writeline(i.tostring()) 

i = i + 1 

loop 

12 字符串连接 

c#: 

string s1; 

string s2 = “hello”; 

s2 += ” world”; 

s1 = s2 + ” !!!”; 

vb: 

dim s1, s2 as string 

s2 = “hello” 

s2 &= ” world” 

s1 = s2 & ” !!!” 

声明事件 

c#: 

void mybutton_click(object sender, 

eventargs e) { 

… 

vb: 

sub mybutton_click(sender as object, 

e as eventargs) 

… 

end sub 

13 声明object 

c# 

myobject obj = (myobject)session[“some value”]; 

imyobject iobj = obj 

vb: 

dim bj as myobject 

dim iobj as imyobject 

obj = session(“some value”) 

iobj = ctype(obj, imyobject) 

14 数据类型转换 

c# 

int i = 3; 

string s = i.tostring(); 

double d = double.parse(s); 

vb: 

dim i as integer 

dim s as string 

dim d as double 

i = 3 

s = i.tostring() 

d = cdbl(s) 

15 类的声明和继承 

c#: 

using system; 

namespace myspace { 

public class foo : bar { 

int x; 

public foo() { x = 4; } 

public void add(int x) { this.x += x; } 

public int getnum() { return x; } 

vb: 

imports system 

namespace myspace 

public class foo : inherits bar 

dim x as integer 

public sub new() 

mybase.new() 

x = 4 

end sub 

public sub add(x as integer) 

me.x = me.x + x 

end sub 

public function getnum() as integer 

return x 

end function 

end class 

end namespace 

16 声明类的主函数 

c#: 

using system; 

public class consolecs { 

public consolecs() { 

console.writeline(“object created”); 

public static void main (string[] args) { 

console.writeline(“hello world”); 

consolecs ccs = new consolecs(); 

vb 

imports system 

public class consolevb 

public sub new() 

mybase.new() 

console.writeline(“object created”) 

end sub 

public shared sub main() 

console.writeline(“hello world”) 

dim cvb as consolevb 

cvb = new consolevb() 

end sub 

end class 

17 标准模块 

c# 

using system; 

public class module { 

public static void main (string[] args) { 

console.writeline(“hello world”); 

vb: 

imports system 

public module consolevb 

public sub main() 

console.writeline(“hello world”) 

end sub 

end module 

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » vb.net和c#语法比较-.NET教程,VB.Net语言
分享到: 更多 (0)