打印本文 打印本文 关闭窗口 关闭窗口
VB.NET and C# 语法比较手册
作者:武汉SEO闵涛  文章来源:敏韬网  点击数3685  更新时间:2009/4/23 19:01:40  文章录入:mintao  责任编辑:mintao

VB.NET and C# Comparison
This is a quick reference guide to highlight some key syntactical differences between VB.NETand C#. Hope you find this useful!
Thank you to Tom Shelton, Fergus Cooney, and others for your input.



Comments
Data Types
Constants
Enumerations
Operators Choices
Loops
Arrays
Functions
Exception Handling Namespaces
Classes / Interfaces
Constructors / Destructors
Objects
Structs Properties
Delegates / Events
Console I/O
File I/O


VB.NET

C#

Comments '''' Single line only
Rem Single line only

// Single line
/* Multiple
    line  */
/// XML comments on single line
/** XML comments on multiple lines */

Data Types

Value Types
Boolean
Byte
Char   (example: "A"c)
Short, Integer, Long
Single, Double
Decimal
Date

Reference Types
Object
String

Dim x As Integer
Console.WriteLine(x.GetType())     '''' Prints System.Int32
Console.WriteLine(TypeName(x))  '''' Prints Integer

'''' Type conversion
Dim numDecimal As Single = 3.5
Dim numInt As Integer
numInt = CType(numDecimal, Integer)   '''' set to 4 (Banker''''s rounding)
numInt = CInt(numDecimal)  '''' same result as CType
numInt = Int(numDecimal)    '''' set to 3 (Int function truncates the decimal)

Value Types
bool
byte, sbyte
char   (example: ''''A'''')
short, ushort, int, uint, long, ulong
float, double
decimal
DateTime   (not a built-in C# type)

Reference Types
object
string

int x;
Console.WriteLine(x.GetType());    // Prints System.Int32
Console.WriteLine(typeof(int));      // Prints System.Int32


// Type conversion
double numDecimal = 3.5;
int numInt = (int) numDecimal;   // set to 3  (truncates decimal)

Constants Const MAX_STUDENTS As Integer = 25 const int MAX_STUDENTS = 25; Enumerations Enum Action
  Start 
  [Stop]   '''' Stop is a reserved word
  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 Console.WriteLine(a)     '''' Prints 1

Console.WriteLine(Status.Pass)     '''' Prints 70

Dim s As Type = GetType(Status)
Console.WriteLine([Enum].GetName(s, Status.Pass))    '''' Prints Pass enum Action {Start, Stop, Rewind, Forward};
enum Status {Flunk = 50, Pass = 70, Excel = 90};

Action a = Action.Stop;
if (a != Action.Start)
  Console.WriteLine(a + " is " + (int) a);    // Prints "Stop is 1"

Console.WriteLine(Status.Pass);    // Prints Pass Operators

Comparison
=  <  >  <=  >=  <>

Arithmetic
+  -  *  /
Mod
(integer division)
(raise to a power)

Assignment
=  +=  -=  *=  /=  \=  ^=  <<=  >>=  &=

Bitwise
And  AndAlso  Or  OrElse  Not  <<  >>

Logical
And  AndAlso  Or  OrElse  Not

Note: AndAlso and OrElse are for short-circuiting logical evaluations

String Concatenation
&

Comparison
==  <  >  <=  >=  !=

Arithmetic
+  -  *  /
(mod)
(integer division if both operands are ints)
Math.Pow(x, y)

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

Bitwise
&  |  ^   ~  <<  >>

Logical
&&  ||   !

Note: && and || perform short-circuit logical evaluations

String Concatenation
+

Choices

greeting = IIf(age < 20, "What''''s up?", "Hello")

'''' One line doesn''''t require "End If", no "Else"
If language = "VB.NET" Then langType = "verbose"

'''' Use : to put two commands on same line
If x <> 100 Then x *= 5 : y *= 2  

'''' or to break up any long single command use _
If whenYouHaveAReally < 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

Select Case color   '''' Must be a primitive data type
  Case "pink", "red"
    r += 1
  Case "blue"
    b += 1
  Case "green"
    g += 1
  Case Else
    other += 1
End Select

greeting = age < 20 ? "What''''s up?" : "Hello";

if (x != 100) {    // Multiple statements must be enclosed in {}
  x *= 5;
  y *= 2;
}

No need for _ or : since ; is used to terminate each statement.




if
(x > 5)
  x *= y;
else if (x == 5)
  x += y;
else if (x < 10)
  x -= y;
else
  x /= y;



switch (color) {                          // Must be integer or string
  case "pink":
  case "red":    r++;    break;        // break is mandatory; no fall-through
  case "blue":   b++;   break;
  case "green": g++;   break;
  default:    other++;   break;       // break necessary on default
}

Loops Pre-test Loops: While c < 10
  c += 1
End While

Do Until c = 10 
  c += 1
Loop

Do While c < 10
  c += 1
Loop

For c = 2 To 10 Step 2
  Console.WriteLine(c)
Next


Post-test Loops: Do 
  c += 1
Loop While c < 10 Do

[1] [2] [3] [4]  下一页

打印本文 打印本文 关闭窗口 关闭窗口