C # VB.NET syntax comparison

xiaoxiao2021-03-30  196

Variable reputation

C # syntax

INT X;

String S;

String S1, S2;

Object O;

Object obj = new object ();

Public String Name;

VB grammar

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 statement

C #:

Response.write ("Chinese C # Technology Station");

VB:

Response.write ("Chinese C # Technology Station")

3. Notes statement

// Chinese C # Technology Station

/ *

Welcome to visit

,

Chinese C # Technology Station

* /

VB:

'Chinese C # Technology Station

4. Get the variable transferred by the 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. Declare attribute

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. array

C #

String [] a = new string [3];

a [0] = "1";

a [1] = "2";

a [2] = "3";

//Two-dimensional array

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 variable initialization

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; judgment statement (if statement) IF (request.queryString! = Null) {

...

}

VB:

If not (Request.QueryString = NULL)

...

END IF

9. Branch statement (CASE statement)

C #:

Switch (firstname) {

Case "john":

...

Break;

Case "Paul":

...

Break;

Case "rinco":

...

Break;

}

VB:

SELECT (FirstName)

Case "john":

...

Case "Paul": ...

Case "rinco":

...

End SELECT

10 for loop statement

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 cycle

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 string connection

C #:

String S1;

String S2 = "Hello";

S2 = "world";

S1 = S2 "!!!";

VB:

DIM S1, S2 AS STRING

S2 = "Hello"

S2 & = "world"

S1 = S2 & "!!!"

Declaration event

C #:

Void MyButton_Click (Object Sender,

Eventargs e) {

...

}

VB:

Sub mybutton_click (sender as object,

E as eventargs)

...

End Sub

13 Declaration 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 data type conversion

C #

INT i = 3;

String s = i.to.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 categories of statements and inheritance

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 () {returnix}

}

}

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 statement of the main function

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 standard module

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

转载请注明原文地址:https://www.9cbs.com/read-130530.html

New Post(0)