Comparison between VB and C # syntax

Source: Internet
Author: User
1. variable name
C #:
Int X;
String S;
String S1, S2;
Object OBJ;
Object OBJ = new object ();
Public string name;
VB syntax
Dim X as integer
Dim s as string
Dim S1, S2 as string
Dim OBJ 'implicitly object
Dim OBJ as new object ()
Public name as string

 

2 Statement
C #:
Response. Write ("123456 ");
VB:
Response. Write ("123456 ")

3. Comment statement
// This is C # Annotation
/*
Multi-Line injection of C #
Release
*/

VB:
'This is a VB comment.

4. Get the variable passed 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 attributes

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 [10];
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 (10) 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 = 10;
Double [] A = {3.14, 4.53, 5.10 };
VB:
Dim s as string = "Hello World"
Dim I as integer = 10
Dim A () as double = {3.14, 4.53, 5.10}

8. Judgment Statement (if Statement)
If (I! = 10 ){
...
}

VB:
If not (I = 10)
...
End if

9. Branch Statement (case statement)
C #:
Switch (){
Case 0:
...
Break;
Case 1:
...
Break;
Case 2:
...
Break;
}
VB:
Select ()
Case 0:
...
Case1:
...
Case 2:
...
End select

10. For Loop statements
C #
For (INT I = 0; I <10; I ++)
A (I) = I;
VB:
Dim I as integer
For I = 0 to 10
A (I) = I
Next

11. While Loop
C #:
Int I = 0;
While (I <10 ){
Console. writeline (I. tostring ());
I ++;
}
VB:
Dim I as integer
I = 0
Do while I <10
Console. writeline (I. tostring ())
I = ++
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 &"!!! "

Declare events
C #:
Void mybutton_click (Object sender, eventargs e ){
...
}
VB:
Sub mybutton_click (sender as object, e as eventargs)
...
End sub

13. Declare an object
C #:
Myobject OBJ = (myobject) session ["xiaoyear"];
Imyobject iobj = OBJ
VB:
Dim BJ as myobject
Dim iobj as imyobject
OBJ = SESSION ("xiaoyear ")
Iobj = ctype (OBJ, imyobject)

14. Data Type Conversion
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. Class declaration and inheritance
C #:
Using system;
Namespace MySpace {
Public Class A: B {
Int X;
Public F () {x = 10 ;}
Public void add (int x) {This. x + = x ;}
Public int getnum () {return X ;}
}
}

VB:
Imports system
Namespace MySpace
Public Class A: inherits B
Dim X as integer
Public sub new ()
Mybase. New ()
X = 10
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. Declare the main function of the 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.