vb.net與C#的語法差異表

  • 15293
  • 0
  • C#
  • 2010-08-25

摘要:vb.net與C#的語法差異表

C#和VB.NET的語法上的差異

VB.NETC#
資料型態
Object
Boolean
Byte
Char
String
Short
Integer
Long
Single , Double
Decimal
Date
object
bool
byte , sbyte
char
string
short
int , uint
long , ulong
float , double
decimal
DateTime
註解方式
'單行註解,在最前方加單引號
REM REM也是VB的單行註解
//單行註解,在最前行方加入兩個斜線
/* 多行註解, 在最前與最後分別加入符號 */
變數宣告
Dim a As Integer
Dim b As Integer = 1
Dim [class] As String = "使用保留字當變數
int a;
int b = 1;
string @class = "使用保留字當變數";
型別
Dim a As Integer = 1
Dim typea As System.Type = a.GetType()
Dim typeb As System.Type = GetType(a)
int a = 1;
System.Type typea = a.GetType();
System.Type typeb = typeof(a);
型別轉換 
Dim a As Integer
Dim b As ClassType = DirectCast( a, ClassType )
Dim c As ClassType = CType( a, ClassType )
Dim d As ClassType = TryCast( a, ClassType )
使用TryCast若轉型失敗會回傳Nothing
int a;
ClassType b = (ClassType)a;
ClassType c = (ClassType)a;
ClassType d = a as ClassType;
使用as關鍵字,若轉型失敗會回傳null
比較運算子 
=
<=
>=
<> 
等於
小於
大於
小於等於
大於等於
不等於
==
<=
>=
!=
等於
小於
大於
小於等於
大於等於
不等於