Complete Comparison for VB.NET and C#






4.87/5 (178 votes)
Mar 30, 2005
11 min read

1601893
This article explains about advantages, differences and new features of VB.NET and C#.
Contents
- Introduction
- Advantages of both languages
- Keyword Differences
- Data types Differences
- Operators Differences
- Programming Difference
- New Features of both languages in 2005 version
- Conclusion
- History
Introduction
Some people like VB.NET's natural language, case-insensitive approach, others like C#'s terse syntax. But both have access to the same framework libraries. We will discuss about the differences in the following topics:
- Advantages of both languages
- Keyword Differences
- Data types Differences
- Operators Differences
- Programming Difference
Advantages of both languages
VB.NET |
C# |
|
|
Keyword Differences
Purpose |
VB.NET |
C# |
Declare a variable |
|
|
Declare a named constant |
|
|
Create a new object |
|
|
Function/method does not return a value |
|
|
Overload a function or method (Visual Basic: overload a procedure or method) |
|
(No language keyword required for this purpose) |
Refer to the current object |
|
|
Make a nonvirtual call to a virtual method of the current object |
|
n/a |
Retrieve character from a string |
|
|
Declare a compound data type (Visual Basic: Structure) |
|
|
Initialize an object (constructors) |
|
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 |
|
destructor |
Initialize a variable where it is declared |
Dim x As Long = 5 Dim c As New _
Car(FuelTypeEnum.Gas)
|
// initialize to a value:
int x = 123;
// or use default
// constructor:
int x = new int();
|
Take the address of a function |
|
|
Declare that an object can be modified asynchronously |
|
|
Force explicit declaration of variables |
|
n/a. (All variables must be declared prior to use) |
Test for an object variable that does not refer to an object |
|
|
Value of an object variable that does not refer to an object |
|
|
Test for a database null expression |
|
n/a |
Test whether a Variant variable has been initialized |
n/a |
n/a |
Define a default property |
|
by using indexers |
Refer to a base class |
|
|
Declare an interface |
|
|
Specify an interface to be implemented |
|
|
Declare a class |
|
|
Specify that a class can only be inherited. An instance of the class cannot be created. |
|
|
Specify that a class cannot be inherited |
|
|
Declare an enumerated type |
|
|
Declare a class constant |
|
|
Derive a class from a base class |
|
|
Override a method |
|
|
Declare a method that must be implemented in a deriving class |
|
|
Declare a method that can't be overridden |
|
|
Declare a virtual method, property (Visual Basic), or property accessor (C#, C++) |
|
|
Hide a base class member in a derived class |
|
n/a |
Declare a typesafe reference to a class method |
|
|
Specify that a variable can contain an object whose events you wish to handle |
|
(Write code - no specific keyword) |
Specify the events for which an event procedure will be called |
|
n/a |
Evaluate an object expression once, in order to access multiple members |
With objExpr
<.member>
<.member>
End With
|
n/a |
Structured exception handling |
Try <attempt>
Catch
<handle errors>
Finally
<always execute>
End Try
|
|
Decision structure (selection) |
|
|
Decision structure (if ... then) |
|
|
Loop structure (conditional) |
|
|
Loop structure (iteration) |
|
|
Declare an array |
Dim a() As Long
|
int[] x = new int[5];
|
Initialize an array |
Dim a() As Long = {3, 4, 5}
|
int[] x = new int[5] {
1, 2, 3, 4, 5};
|
Reallocate array |
|
n/a |
Visible outside the project or assembly |
|
|
Invisible outside the assembly (C#/Visual Basic) or within the package (Visual J#, JScript) |
|
|
Visible only within the project (for nested classes, within the enclosing class) |
|
|
Accessible outside class and project or module |
|
|
Accessible outside the class, but within the project |
|
|
Only accessible within class or module |
|
|
Only accessible to current and derived classes |
|
|
Preserve procedure's local variables |
|
n/a |
Shared by all instances of a class |
|
|
Comment code |
|
|
Case-sensitive? |
No |
Yes |
Call Windows API |
|
use Platform Invoke |
Declare and raise an event |
|
|
Threading primitives |
|
|
Go to |
|
|
Data types Differences
Purpose/Size |
VB.NET |
C# |
Decimal |
|
|
Date |
|
|
(varies) |
|
|
1 byte |
|
|
2 bytes |
|
|
2 bytes |
|
|
4 bytes |
|
|
8 bytes |
|
|
4 bytes |
|
|
8 bytes |
|
|
Operators Differences
Purpose |
VB.NET |
C# |
Integer division |
|
|
Modulus (division returning only the remainder) |
|
|
Exponentiation |
|
n/a |
Integer division Assignment |
|
|
Concatenate |
|
|
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 |
|
|
Compare object reference type |
|
|
Concatenate strings |
|
|
Shortcircuited Boolean AND |
|
|
Shortcircuited Boolean OR |
|
|
Scope resolution |
|
|
Array element |
|
|
Type cast |
|
|
Postfix increment |
n/a |
|
Postfix decrement |
n/a |
|
Indirection |
n/a |
|
Address of |
|
|
Logical-NOT |
|
|
One's complement |
|
|
Prefix increment |
n/a |
|
Prefix decrement |
n/a |
|
Size of type |
n/a |
|
Bitwise-AND |
|
|
Bitwise-exclusive-OR |
|
|
Bitwise-inclusive-OR |
|
|
Logical-AND |
|
|
Logical-OR |
|
|
Conditional |
|
|
Pointer to member |
n/a |
|
Programming Difference
Purpose |
VB.NET |
C# |
Declaring Variables |
Dim x As Integer
Public x As Integer = 10 |
int x;
int x = 10;
|
Comments |
' comment
x = 1 ' comment
Rem comment |
// comment
/* multiline
comment */
|
Assignment Statements |
nVal = 7 |
nVal = 7; |
Conditional Statements |
If nCnt <= nMax Then
' Same as nTotal =
' nTotal + nCnt.
nTotal += nCnt
' Same as nCnt = nCnt + 1.
nCnt += 1
Else
nTotal += nCnt
nCnt -= 1
End If
|
if (nCnt <= nMax)
{
nTotal += nCnt;
nCnt++;
}
else
{
nTotal +=nCnt;
nCnt--;
}
|
Selection Statements |
Select Case n
Case 0
MsgBox ("Zero")
' Visual Basic .NET exits
' the Select at
' the end of a Case.
Case 1
MsgBox ("One")
Case 2
MsgBox ("Two")
Case Else
MsgBox ("Default")
End Select
|
switch(n)
{
case 0:
Console.WriteLine("Zero");
break;
case 1:
Console.WriteLine("One");
break;
case 2:
Console.WriteLine("Two");
break;
default:
Console.WriteLine("?");
break;
}
|
FOR Loops |
For n = 1 To 10
MsgBox("The number is " & n)
Next
For Each prop In obj
prop = 42
Next prop
|
for (int i = 1; i <= 10; i++)
Console.WriteLine(
"The number is {0}", i);
foreach(prop current in obj)
{
current=42;
}
|
Hiding Base Class Members |
Public Class BaseCls
' The element to be shadowed
Public Z As Integer = 100
public Sub Test()
System.Console.WriteLine( _
"Test in BaseCls")
End Sub
End Class
Public Class DervCls
Inherits BaseCls
' The shadowing element.
Public Shadows Z As String = "*"
public Shadows Sub Test()
System.Console.WriteLine( _
"Test in DervCls")
End Sub
End Class
Public Class UseClasses
' DervCls widens to BaseCls.
Dim BObj As BaseCls =
New DervCls()
' Access through derived
' class.
Dim DObj As DervCls =
New DervCls()
Public Sub ShowZ()
System.Console.WriteLine( _
"Accessed through base "&_
"class: " & BObj.Z)
System.Console.WriteLine(_
"Accessed through derived "&_
"class: " & DObj.Z)
BObj.Test()
DObj.Test()
End Sub
End Class
|
public class BaseCls
{
// The element to be hidden
public int Z = 100;
public void Test()
{
System.Console.WriteLine(
"Test in BaseCls");
}
}
public class DervCls : BaseCls
{
// The hiding element
public new string Z = "*";
public new void Test()
{
System.Console.WriteLine(
"Test in DervCls");
}
}
public class UseClasses
{
// DervCls widens to BaseCls
BaseCls BObj = new DervCls();
// Access through derived
//class
DervCls DObj = new DervCls();
public void ShowZ()
{
System.Console.WriteLine(
"Accessed through " +
"base class: {0}",
BObj.Z);
System.Console.WriteLine(
"Accessed through" +
" derived class:{0}",
DObj.Z);
BObj.Test();
DObj.Test();
}
} |
WHILE Loops |
' Test at start of loop
While n < 100 .
' Same as n = n + 1.
n += 1
End While '
|
while (n < 100)
n++;
|
Parameter Passing by Value |
' The argument Y is
'passed by value.
Public Sub ABC( _
ByVal y As Long)
'If ABC changes y, the
' changes do not affect x.
End Sub
ABC(x) ' Call the procedure.
' You can force parameters to
' be passed by value,
' regardless of how
' they are declared,
' by enclosing
' the parameters in
' extra parentheses.
ABC((x))
|
/* Note that there is
no way to pass reference
types (objects) strictly
by value. You can choose
to either pass the reference
(essentially a pointer), or
a reference to the reference
(a pointer to a pointer).*/
// The method:
void ABC(int x)
{
...
}
// Calling the method:
ABC(i);
|
Parameter Passing by Reference |
Public Sub ABC(ByRef y As Long)
' The parameter y is declared
'by referece:
' If ABC changes y, the changes are
' made to the value of x.
End Sub
ABC(x) ' Call the procedure.
|
/* Note that there is no
way to pass reference types
(objects) strictly by value.
You can choose to either
pass the reference
(essentially a pointer),
or a reference to the
reference (a pointer to a
pointer).*/
// Note also that unsafe C#
//methods can take pointers
//just like C++ methods. For
//details, see unsafe.
// The method:
void ABC(ref int x)
{
...
}
// Calling the method:
ABC(ref i);
|
Structured Exception Handling |
Try
If x = 0 Then
Throw New Exception( _
"x equals zero")
Else
Throw New Exception( _
"x does not equal zero")
End If
Catch err As System.Exception
MsgBox( _
"Error: " & Err.Description)
Finally
MsgBox( _
"Executing finally block.")
End Try
|
// try-catch-finally
try
{
if (x == 0)
throw new System.Exception(
"x equals zero");
else
throw new System.Exception(
"x does not equal zero");
}
catch (System.Exception err)
{
System.Console.WriteLine(
err.Message);
}
finally
{
System.Console.WriteLine(
"executing finally block");
}
|
Set an Object Reference to Nothing |
o = Nothing |
o = null; |
Initializing Value Types |
Dim dt as New System.DateTime( _
2001, 4, 12, 22, 16, 49, 844)
|
System.DateTime dt = new System.DateTime( 2001, 4, 12, 22, 16, 49, 844); |
New Features of both languages in 2005 version
VB.NET |
C# |
Visual Basic 2005 has many new and improved language features -- such as inheritance, interfaces, overriding, shared members, and overloading -- that make it a powerful object-oriented programming language. As a Visual Basic developer, you can now create multithreaded, scalable applications using explicit multithreading. This language has following new features,
|
With the release of Visual Studio 2005, the C# language has been updated to version 2.0. This language has following new features:
|
Conclusion
I think that this article will help you understand both language features and also you can choose a language based on your taste. I will update this article in future if there are any changes.
History
- 14 Apr 2005 - Topic added - "New features of both languages in 2005 version"
- 30 Mar 2005 - Initial version.