c#和vb.net语法对比图_c#教程

2008-02-23 05:45:07来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

C#和VB.net的语法相差还是比较大的. 可能您会C#,可能您会VB.

将他们俩放在一起对比一下您就会很快读懂,并掌控另一门语言.

相信下面这张图会对您帮助很大.

Comments

VB.NET

Single line only

Rem Single line only

C#

// Single line

/* Multiple

line */

/// XML comments on single line

/** XML comments on multiple lines */

Data Types

VB.NET

Value Types

Boolean

Byte

Char (example: "A")

Short, Integer, Long

Single, Double

Decimal

Date 





Reference Types

Object

String







Dim x As Integer

System.Console.WriteLine(x.GetType())

System.Console.WriteLine(TypeName(x)) 





Type conversion

Dim d As Single = 3.5

Dim i As Integer = CType (d, Integer)

i = CInt (d)

i = Int(d)

C#

//Value Types

bool

byte, sbyte

char (example: A)

short, ushort, int, uint, long, ulong

float, double

decimal

DateTime 





//Reference Types

object

string







int x;

Console.WriteLine(x.GetType())

Console.WriteLine(typeof(int)) 





//Type conversion

float d = 3.5;

int i = (int) d

Constants

VB.NET

Const MAX_AUTHORS As Integer = 25

ReadOnly MIN_RANK As Single = 5.00

C#

const int MAX_AUTHORS = 25;

readonly float MIN_RANKING = 5.00;

Enumerations

VB.NET

Enum Action

  Start

  Stop is a reserved word

[Stop]

  Rewind

  Forward

End Enum



Enum Status

   Flunk = 50

   Pass = 70

   Excel = 90

End Enum



Dim a As Action = Action.Stop 

If a <> Action.Start Then _

Prints "Stop is 1" 

   System.Console.WriteLine(a.ToString & " is " & a)



Prints 70

System.Console.WriteLine(Status.Pass)

Prints Pass

System.Console.WriteLine(Status.Pass.ToString())

C#

enum Action {Start, Stop, Rewind, Forward};

enum Status {Flunk = 50, Pass = 70, Excel = 90};























Action a = Action.Stop;

if (a != Action.Start)

//Prints "Stop is 1"

  System.Console.WriteLine(a   " is "   (int) a); 



// Prints 70

System.Console.WriteLine((int) Status.Pass); 

// Prints Pass

System.Console.WriteLine(Status.Pass);

Operators

VB.NET

Comparison

=  <  >  <=  >=  <> 





Arithmetic

   -  *  /

Mod

  (integer division)

^  (raise to a power) 





Assignment

=   =  -=  *=  /=  =  ^=  <<=  >>=  &= 





Bitwise

And  AndAlso  Or  OrElse  Not  <<  >> 





Logical

And  AndAlso  Or  OrElse  Not 





String Concatenation

& 

C#

//Comparison

==  <  >  <=  >=  != 





//Arithmetic

   -  *  /

%  (mod)

/  (integer division if both operands are ints)

Math.Pow(x, y) 





//Assignment

=   =  -=  *=  /=   %=  &=  |=  ^=  <<=  >>=      -- 





//Bitwise

&  |  ^   ~  <<  >> 





//Logical

&&  ||   ! 





//String Concatenation

 

Choices

VB.NET

greeting = IIf(age < 20, "Whats up?", "Hello") 





One line doesnt require "End If", no "Else"

If language = "VB.NET" Then langType = "verbose" 





Use: to put two commands on same line

If x <> 100 And y < 5 Then x *= 5 : y *= 2   





Preferred

If x <> 100 And y < 5 Then

  x *= 5

  y *= 2

End If 









or to break up any long single command use _

If henYouHaveAReally < longLine And _ 

itNeedsToBeBrokenInto2   > Lines  Then _

  UseTheUnderscore(charToBreakItUp) 





If x > 5 Then

  x *= y 

ElseIf x = 5 Then

  x  = y 

ElseIf x < 10 Then

  x -= y

Else

  x /= y

End If 





Must be a primitive data type

Select Case color   

  Case "black", "red"

    r  = 1

  Case "blue"

    b  = 1

  Case "green"

    g  = 1

  Case Else

    other  = 1

End Select

C#

greeting = age < 20 ? "Whats up?" : "Hello"; 























if (x != 100 && y < 5)

{

  // Multiple statements must be enclosed in {}

  x *= 5;

  y *= 2;

} 

















if (x > 5) 

  x *= y; 

else if (x == 5) 

  x  = y; 

else if (x < 10) 

  x -= y; 

else 

  x /= y;







//Must be integer or string

switch (color)

{

  case "black":

  case "red":    r  ;

   break;

  case "blue"

   break;

  case "green": g  ;  

   break;

  default:    other  ;

   break;

}

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇: c#的四个基本技巧 _c#教程

下一篇: c# socket编程_c#应用