VB.net and C # syntax comparisons
Last Update:2017-02-28
Source: Internet
Author: User
Comparison | Grammar because some people have some confusion about vb.net and C # choice, it's just the grammar habit problem, I put the grammar listed to compare, we have a perceptual knowledge.
1. 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 statements
C#:
Response.Write ("Chinese C # Technology station");
Vb:
Response.Write ("Chinese C # Technical station")
3. Comment Statement
Chinese C # Technology station
/*
Welcome to visit
,
Chinese C # Technology station
*/
Vb:
' Chinese C # technology station
4. Get the URL pass variable
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. Declaring properties
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 "Ringo":
...
Break
}
Vb:
Select (FirstName)
Case "John":
...
Case "Paul": ...
Case "Ringo":
...
End Select
A 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
One while loop
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 & "!!!"
declaring events
C#:
void MyButton_Click (Object sender,
EventArgs E) {
...
}
Vb:
Sub MyButton_Click (Sender as Object,
E as EventArgs)
...
End Sub
13 Declaring 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 Conversions
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 declaration and inheritance of classes
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 declaring the main function of a class
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 Modules
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