日期:2012-10-27  浏览次数:20474 次

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"