快捷搜索:

C#与VB语法的比较

  现在ASP+支持三种内建语言:C#,Visual Basic 以及JScript.

  下面的例子中对C#与VB的语法,进行了对比。如果需要更多的关于其它语言语法的信息,请参照完整的NGWS SDK文档。你可以从下面的对比中看到C#与VB的不同:

C# 语法 VB 语法
变量声明  

int x; String s; String s1, s2; Object o; Object obj = new Object(); public String name;

 

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

 
语句  

Response.Write("foo");

 

Response.Write("foo")

 
注释  

// This is a comment /* This is a multi-line comment */

 

" This is a comment " This " is " a " multi-line " comment

 
访问索引属性  

String s = Request.QueryString["Name"]; String value = Request.Cookies["key"];

 

Dim s, value As String s = Request.QueryString("Name") value = Request.Cookies("Key").Value "Note that default non-indexed properties "must be explicitly named in VB

 
声明一个属性  

public String name { get { ... return ...; } set { ... = value; } /div>  

Public Property Name As String Get ... Return ...; End Get Set ... = Value; End Set End Property

 
数组  

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";

 

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" " Array of unspecified bounds (NA in C#) 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"

 
初始化  

String s = "Hello World"; int i = 1 double[] a = { 3.00, 4.00, 5.00 };

 

Dim s As String = "Hello World" Dim i As Integer = 1 Dim a() As Double = { 3.00, 4.00, 5.00 }

 
IF 语句  

if (Request.QueryString != null) { ... }

 

If Not (Request.QueryString = Null) ... End If

 
Case 语句  

switch (FirstName) { case "John" : ... break; case "Paul" : ... break; case "Ringo" : ... break; }

 

Select (FirstName) case "John" : ... case "Paul" : ... case "Ringo" : ... End Select

 

1

作者:asp中华网责任编辑:)

天极新媒体 最酷科技资讯

扫码赢大奖

评论

* 网友发言均非本站立场,本站不在评论栏推荐任何网店、经销商,谨防上当受骗!

您可能还会对下面的文章感兴趣: