A Comprehensive Comparison between VB.Net and C#

Both VB.Net and C# are very popular. When some people like the natural language and case-insensitive approach of VB.Net some other’s like the crispy syntax of C#. However, both have access to the same frameworks. Here is a comparison between VB.Net and C#.

 VB.NET and C# : Advantages of Both Languages

VB.Net

C#
Some optional parameters are supported. Easy interoperability. Xml documentation will be generated from course code comments
Supporting late binding option strict off-type safety and legacy libraries are easy to use. Operator overloading. In VBNet i is in Whidbey,
Named indexers are supported Language support for unsigned types
Various functions are available but most of them may turn harmful if not used wisely Presence of ‘The Using’ statement to make the disposal of unmanaged resource simple.
The ‘with’ construct. It is a difference certainly if not an advantage. Explicit interface implementation
Event handling is simpler
Interfaces with different method names can be implemented
Availability of Catch… When… clauses
Compiles the code in background

 

Keyword Differences

Purpose

VB.NET

C#

Declare a variable Private, Public, Friend, Protected, Static1, Shared, Dim declarators (keywords include user-defined types and built-in types)
Declare a named constant Const const
Create a new object New, CreateObject() new
Function/method does not return a value Sub void
Overload a function or method (Visual Basic: overload a procedure or method) Overloads (No language keyword required for this purpose)
Refer to the current object Me this
Make a nonvirtual call to a virtual method of the current object MyClass n/a
Retrieve character from a string GetChar Function []
Declare a compound data type (Visual Basic: Structure) Structure <members> End Structure struct, class, interface
Initialize an object (constructors) Sub New() Constructors, or system default type constructors
Terminate an object directly n/a n/a
Method called by the system just before garbage collection reclaims an object7 Finalize destructor
Initialize a variable where it is declared Hide   Copy Code

Dim x As Long = 5

Hide   Copy Code

Dim c As New _
Car(FuelTypeEnum.Gas)

Hide   Copy Code

// initialize to a value:
int x = 123;
// or use default
// constructor:
int x = new int();

Take the address of a function AddressOf (For class members, this operator returns a reference to a function in the form of a delegate instance) delegate
Declare that an object can be modified asynchronously n/a volatile
Force explicit declaration of variables Option Explicit n/a. (All variables must be declared prior to use)
Test for an object variable that does not refer to an object obj = Nothing obj == null
Value of an object variable that does not refer to an object Nothing null
Test for a database null expression IsDbNull n/a
Test whether a Variant variable has been initialized n/a n/a
Define a default property Default by using indexers
Refer to a base class MyBase base
Declare an interface Interface interface
Specify an interface to be implemented Implements (statement) class C1 : I1
Declare a class Class <implementation> class
Specify that a class can only be inherited. An instance of the class cannot be created. MustInherit abstract
Specify that a class cannot be inherited NotInheritable sealed
Declare an enumerated type Enum <members> End Enum enum
Declare a class constant Const const (Applied to a field declaration)
Derive a class from a base class Inherits C2 class C1 : C2
Override a method Overrides override
Declare a method that must be implemented in a deriving class MustOverride abstract
Declare a method that can’t be overridden NotOverridable (Methods are not overridable by default.) sealed
Declare a virtual method, property (Visual Basic), or property accessor (C#, C++) Overridable virtual
Hide a base class member in a derived class Shadowing n/a
Declare a typesafe reference to a class method Delegate delegate
Specify that a variable can contain an object whose events you wish to handle WithEvents (Write code – no specific keyword)
Specify the events for which an event procedure will be called Handles (Event procedures can still be associated with a WithEventsvariable by naming pattern.) n/a
Evaluate an object expression once, in order to access multiple members Hide   Copy Code

With objExpr
<.member>
<.member>
End With

n/a
Structured exception handling Hide   Copy Code

Try <attempt>
Catch
<handle errors>
Finally
<always execute>
End Try

try, catch, finally,throw
Decision structure (selection) Select Case …, Case, Case Else, End Select switch, case, default, goto, break
Decision structure (if … then) If … Then, ElseIf … Then, Else, End If if, else
Loop structure (conditional) While, Do [While, Until] …, Loop [While, Until] do, while, continue
Loop structure (iteration) For …, [Exit For], Next<BR>For Each …, [Exit For,] Next for, foreach
Declare an array Hide   Copy Code

Dim a() As Long

Hide   Copy Code

int[] x = new int[5];

Initialize an array Hide   Copy Code

Dim a() As Long = {3, 4, 5}

Hide   Copy Code

int[] x = new int[5] {
1, 2, 3, 4, 5};

Reallocate array Redim n/a
Visible outside the project or assembly Public public
Invisible outside the assembly (C#/Visual Basic) or within the package (Visual J#, JScript) Friend internal
Visible only within the project (for nested classes, within the enclosing class) Private private
Accessible outside class and project or module Public public
Accessible outside the class, but within the project Friend internal
Only accessible within class or module Private private
Only accessible to current and derived classes Protected protected
Preserve procedure’s local variables Static n/a
Shared by all instances of a class Shared static
Comment code

Rem

//, /* */ for multi-line comments

/// for XML comments

Case-sensitive? No Yes
Call Windows API Declare <API> use Platform Invoke
Declare and raise an event Event, RaiseEvent event
Threading primitives SyncLock lock
Go to Goto goto

Data Type Differences

Purpose/Size

VB.NET

C#

Decimal Decimal decimal
Date Date DateTime
(varies) String string
1 byte Byte byte
2 bytes Boolean bool
2 bytes Short, Char (Unicode character) short, char (Unicode character)
4 bytes Integer int
8 bytes Long long
4 bytes Single float
8 bytes Double double

Operational Differences

Purpose

VB.NET

C#

Integer division \ /
Modulus (division returning only the remainder) Mod %
Exponentiation ^ n/a
Integer division Assignment \= /=
Concatenate &= NEW +=
Modulus n/a %=
Bitwise-AND n/a &=
Bitwise-exclusive-OR n/a ^=
Bitwise-inclusive-OR n/a |=
Equal = ==
Not equal <> !=
Compare two object reference variables Is ==
Compare object reference type TypeOf x Is Class1 x is Class1
Concatenate strings & +
Shortcircuited Boolean AND AndAlso &&
Shortcircuited Boolean OR OrElse ||
Scope resolution . . and base
Array element () [ ]
Type cast Cint, CDbl, …, CType (type)
Postfix increment n/a ++
Postfix decrement n/a
Indirection n/a * (unsafe mode only)
Address of AddressOf & (unsafe mode only; also see fixed)
Logical-NOT Not !
One’s complement Not ~
Prefix increment n/a ++
Prefix decrement n/a
Size of type n/a sizeof
Bitwise-AND And &
Bitwise-exclusive-OR Xor ^
Bitwise-inclusive-OR Or |
Logical-AND And &&
Logical-OR Or ||
Conditional If Function () ?:
Pointer to member n/a . (Unsafe mode only)

 

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *