Home
Contact us
Technology Skills
Comparisions
Interview Q & A
Management Skills
Free SMS Collection
Entertainment
Set as Homepage | Add to favorites


Bestest Blog of All- Time

C# v/s VB.NET Comparision

Table of Contents



1. Syntax Differences

1.1 Case Sensitivity

1.2 Statement Delimiters

1.3 Statement Blocks

1.4 Comments

1.5 Variable Declarations

1.6 Data Type Names

1.7 Special Operators

1.8 Redimensioning of Arrays

1.9 Self Documenting Code

1.10 Operator Overloading

2. Managed and Unmanaged code

2.1 Writing Unmanaged Code

2.2 COM Interoperability



3. References


1. SYNTAX DIFFERENCES



CASE SENSITIVITY

C# is case-sensitive while VB.NET is not


STATEMENT DELIMITERS

Statement delimiters are the delimiters used to separate each statement from one another.

In C#, the semicolon is the statement delimiter. In VB.NET, each statement is delimited by a carraige return, meaning, essentially, each line of code goes on its own line.


STATEMENT BLOCKS

A block is a set of grouped statements. For example, if you have an If statement in VB.NET with the following form:

If booleanValue then

Statement1

Statement2

...

StatementN

End If


The statements Statement1 through StatmentN are all in a block. Note that the block is marked by the beginning If statement and the closing End If. In C#, curly braces are used as block delimiters. That is, in C# our if code would look like:

if (booleanValue)

{

Statement1

Statement2

...

StatementN

}

COMMENTS

In VB.NET we specify commented lines using the apostrophe ('); in C# we can have single line comments, marked by //, or we can create mutli-line comments using the delimiters /* and */. That is, everything between those delimiters is considered a comment.

VARIABLE DECLARATIONS

In VB.NET variables are declared via the Dim statement, in the following syntax:

Dim VariableName as Type [ = value] < /FONT >


In C#, the syntax is just a little different. We omit the Dim statement and place the Type before the VariableName, like so:

Type VariableName [ = value];

// an example
int iAge = 23; < /FONT >

DATA TYPE NAMES

Differences in Data Type Names

Visual C# .NET also supports the signed byte, unsigned short, unsigned int, and unsigned long data types, which are not available in Visual Basic .NET.



Visual Basic .NET Visual C# .NET .NET Framework
Boolean bool System.Boolean
Byte byte System.Byte
Short short System.Int16
Integer int System.Int32
Long long System.Int64
Single float System.Single
Double double System.Double
Decimal decimal System.Decimal
Date System.DateTime System.DateTime
String string System.String
Char char System.Char
Object object System.Object
n/a sbyte System.Sbyte
n/a ushort System.UInt16
n/a uint System.UInt32
n/a ulong System.UInt64

SPECIAL OPERATORS
C# includes ?: (conditional assignment), ++ (increment) and – (decrement) operators.
These Operators are not available in VB.NET.

REDIMENSIONING AN ARRAY
Redim Preserve

Visual Basic.NET allows you to redimension an array while preserving the current contents using the Redim Preserve statement.
This is not available in C#.

SELF DOCUMENTING CODE
C# allows you to generate HTML documentation for your code out of the comments you write inside your code file.

This feature is not available in VB.NET

OPERATOR OVERLOADING

C# Supports Operator Overloading, VB.NET doesn’t.



Struct Vector

{

public double x, y, z;

public Vector(double x, double y, double z)

{

this.x = x;

this.y = y;

this.z = z;

}

public Vector(Vector rhs)

{

x = rhs.x;

y = rhs.y;

z = rhs.z;

}

public override string ToString()

{

return “(“ + x + “, “ + y “, “ + z “ )”;

}

public static Vector operator + (Vector lhs, Vector rhs)

{

Vector result = new Vector(lhs);

result.x += rhs.x;

result.y += rhs.y;

result.z += rhs.z;

return result;

}

}



// Usage

Vector vect1, vect2, vect3;

.

.

.

vect3 = vect1 + vect2;

.

.

2. MANAGED AND UNMANAGED CODE
WRITING UNMANAGED CODE
C# permits you to write unmanaged code. In unmanaged code, you can do things such as declare and operate on pointers, perform conversions between pointers and integral types, and take the address of variables. In a sense, writing unmanaged code is much like writing Visual C code in a Visual C# .NET program.

Because code that is written by using an unmanaged context cannot be verified to be safe, it is run only when the code is fully trusted. Do not use unmanaged context to try to write Visual C code in Visual C# .NET. Unmanaged code must be clearly marked with the modifier unsafe so that developers cannot use unmanaged features accidentally, and the execution engine works to make sure that unmanaged code cannot be run in a non-trusted environment. The scope of the unmanaged context extends from the parameter list to the end of the function, so pointers can also be used in the parameter list.

In Visual Basic .NET, you cannot write unmanaged code, but can call it. Please note that writing unmanaged code and calling unmanaged code are different things all together.

COM INTEROPERABILITY

.NET provides three types of interoperability with unmanaged code:

1. The ability to call a COM component from .NET

2. The ability to call a .NET component from COM and

3. The ability to call API functions from .NET.

When you call unmanaged COM components from .NET, the runtime creates a proxy component for the COM object called a runtime callable wrapper (RCW). The RCW handles all the interaction between .NET code and the COM component. When you call .NET components from COM, the runtime creates a proxy called a COM callable wrapper (CCW).

.NET uses Platform Invocation to allow .NET code to make calls to APIs, such as calls to the Windows operating system's API functions.

Since all types of COM Interoperability is handled by the CLR, it doesn’t matter whether you are using C# or VB.NET.


REFERENCES:
http://support.microsoft.com

This Document does not give a complete end-to-end comparison between C# and VB.NET. This is only meant to help developers who are moving from VB.NET to C# and vice versa.