日期:2012-12-24  浏览次数:20546 次

由于一些人对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 "