Thursday, October 25, 2007

C# AND 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
1.11 Keywords

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]
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 exampleint iAge = 23;

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;
.
.
KEYWORDS

The following table lists the keywords that Visual Basic .NET and Visual C# .NET use in several categories. This information can also be found in the Visual Studio .NET online documentation.

Purpose Visual Basic .NET Visual C# .NET

Object Oriented Programming
Indicates a class constructor Public Class Class1
Public Sub New(..)
MyBase.New

End Sub

End Class
Note: You have to call the base class constructor explicitly in Visual Basic .NET.
public class Class1
{
public Class1(..)
{

}
….
}
Note: The call to the base class constructor (base()) is generated automatically by the compiler in Visual C# .NET if you do not include constructor initializers.


Indicates a class destructor
Note: The Destructor or Finalize method is called by garbage collection.
VB.NET
Protected Overrides Sub Finalize()
m_Gadget = Nothing
m_Gear = Nothing
MyBase.Finalize()
End Sub

C#
public class Class1
{
public ~Class1()
{
….
}
}


Declares a class
C# AND 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
1.11 Keywords

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]
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 exampleint iAge = 23;

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;
.
.
KEYWORDS

The following table lists the keywords that Visual Basic .NET and Visual C# .NET use in several categories. This information can also be found in the Visual Studio .NET online documentation.

Purpose
Visual Basic .NET
Visual C# .NET


Object Oriented Programming




Indicates a class constructor
Public Class Class1
Public Sub New(..)
MyBase.New

End Sub

End Class
Note: You have to call the base class constructor explicitly in Visual Basic .NET.
public class Class1
{
public Class1(..)
{

}
….
}
Note: The call to the base class constructor (base()) is generated automatically by the compiler in Visual C# .NET if you do not include constructor initializers.


Indicates a class destructor
Note: The Destructor or Finalize method is called by garbage collection.
Protected Overrides Sub Finalize()
m_Gadget = Nothing
m_Gear = Nothing
MyBase.Finalize()
End Sub

public class Class1
{
public ~Class1()
{
….
}
}


Declares a class
Class
class


Indicates class inheritance
Public Class A
Inherits B

End Class
public class A : B
{

}


Indicates that the class can only be inherited and cannot be instantiated
MustInherit
abstract


Indicates that the class cannot be inherited
NotInheritable
sealed


Calls your own implementation of the method instead of an overridden method in the derived class
MyClass
None



Refers to a base class from the derived class
MyBase
base


Declares a type-safe reference to a class method
Delegate
delegate


Indicates that the method or the property overrides the implementation in its base class
Overrides
override


Indicates that these methods have no implementation and must be implemented in derived classes
MustOverride
(in MustInherit
class)
abstract
(in abstract
class)


Indicates that the method or the property cannot be overridden in derived classes
NotOverridable
Note: By default, methods are not overridable.
sealed


Indicates that the method or the property can be overridden in an inheriting class
Overridable
virtual


Overloads a procedure, a function, or a method
Overloads
None. Define functions with same name but different signatures.


Specifies that a variable can contain an object whose events you want to handle
WithEvents
No specific keyword


Specifies the events for which an event procedure will be called
Handles (Event procedures can still be associated with a WithEvents variable by naming pattern.)
n/a


Evaluates an object expression one time to access multiple members
With objExpr
<.member>
<.member>
End With
n/a


Refers to the current object
Me
This


Declares an enumerated type
Enum

End Enum
Enum


Declares an interface
Interface
interface


Implements an interface
Implements
class C1 : I1


Indicates an indexer
Default Property
public string this[int index]
{
get {return List[index];}
set {List[index]=value;}
}


Class Access Modifiers




Indicates that the modifier is accessible outside the project or the assembly
Public
public


Indicates that the modifier is accessible inside the assembly only
Friend
internal


Indicates that the modifier is accessible only in the project (for nested classes, in the enclosing class)
Private
private


Class Member Access Modifiers




Indicates that the modifier is accessible outside the class and the project
Public
public


Indicates that the modifier is accessible outside the class, but in the project
Friend
internal


Indicates that the modifier is only accessible in a class or a module
Private
private


Indicates that the modifier is accessible only to current and derived classes
Protected
protected


Indicates the union of Protected and Friend or Internal
Protected Friend
protected internal


Indicates that the members are shared across all instances
Shared
static


Miscellaneous Lifetime




Preserves the local variables for the procedure
Static
n/a


Other



Calls the Windows API
Declare statement
use Platform Invoke

Indicates a comment
‘, Rem
//, /* */ for miltine comments,
/// for XML comments

Indicates a constant
Const
Const, readonly


Creates a new object
New, CreateObject
new

Declares a function or a method with no return value
Sub
void

Declares that an object can be modified asynchronously
n/a
volatile

Declares a variable
Private, Public, Friend, Protected, Static, Shared, Dim
declarators (keywords include user-defined types and built-in types)

Declares a variable explicitly
Option Explicit
None (All variables must be declared before use)

Declares and raises an event
Event, RaiseEvent
event

Declares a structure
Structure

End Structure
struct

Defines a default property
Default
by using indexers

Declares a null object
Nothing
null

Declares a namespace
Namespace

End Namespace
Namespace
{

}

Indicates namespace usage
Imports
using

Retrieves a character from a string
GetChar Function
[ ]

Returns 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

Tests for a null object
Obj Is Nothing
obj == null

Tests for a database null expression
IsDbNull
n/a

Threads primitives
SyncLock
lock










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.



[CodeEverywhere]
geovisit();

Class
class


Indicates class inheritance
Public Class A
Inherits B

End Class
public class A : B
{

}


Indicates that the class can only be inherited and cannot be instantiated
MustInherit
abstract


Indicates that the class cannot be inherited
NotInheritable
sealed


Calls your own implementation of the method instead of an overridden method in the derived class
MyClass
None



Refers to a base class from the derived class
MyBase
base


Declares a type-safe reference to a class method
Delegate
delegate


Indicates that the method or the property overrides the implementation in its base class
Overrides
override


Indicates that these methods have no implementation and must be implemented in derived classes
MustOverride
(in MustInherit
class)
abstract
(in abstract
class)


Indicates that the method or the property cannot be overridden in derived classes
NotOverridable
Note: By default, methods are not overridable.
sealed


Indicates that the method or the property can be overridden in an inheriting class
Overridable
virtual


Overloads a procedure, a function, or a method
Overloads
None. Define functions with same name but different signatures.


Specifies that a variable can contain an object whose events you want to handle
WithEvents
No specific keyword


Specifies the events for which an event procedure will be called
Handles (Event procedures can still be associated with a WithEvents variable by naming pattern.)
n/a


Evaluates an object expression one time to access multiple members
With objExpr
<.member>
<.member>
End With
n/a


Refers to the current object
Me
This


Declares an enumerated type
Enum

End Enum
Enum


Declares an interface
Interface
interface


Implements an interface
Implements
class C1 : I1


Indicates an indexer
Default Property
public string this[int index]
{
get {return List[index];}
set {List[index]=value;}
}


Class Access Modifiers




Indicates that the modifier is accessible outside the project or the assembly
Public
public


Indicates that the modifier is accessible inside the assembly only
Friend
internal


Indicates that the modifier is accessible only in the project (for nested classes, in the enclosing class)
Private
private


Class Member Access Modifiers




Indicates that the modifier is accessible outside the class and the project
Public
public


Indicates that the modifier is accessible outside the class, but in the project
Friend
internal


Indicates that the modifier is only accessible in a class or a module
Private
private


Indicates that the modifier is accessible only to current and derived classes
Protected
protected


Indicates the union of Protected and Friend or Internal
Protected Friend
protected internal


Indicates that the members are shared across all instances
Shared
static


Miscellaneous Lifetime




Preserves the local variables for the procedure
Static
n/a


Other



Calls the Windows API
Declare statement
use Platform Invoke

Indicates a comment
‘, Rem
//, /* */ for miltine comments,
/// for XML comments

Indicates a constant
Const
Const, readonly


Creates a new object
New, CreateObject
new

Declares a function or a method with no return value
Sub
void

Declares that an object can be modified asynchronously
n/a
volatile

Declares a variable
Private, Public, Friend, Protected, Static, Shared, Dim
declarators (keywords include user-defined types and built-in types)

Declares a variable explicitly
Option Explicit
None (All variables must be declared before use)

Declares and raises an event
Event, RaiseEvent
event

Declares a structure
Structure

End Structure
struct

Defines a default property
Default
by using indexers

Declares a null object
Nothing
null

Declares a namespace
Namespace

End Namespace
Namespace
{

}

Indicates namespace usage
Imports
using

Retrieves a character from a string
GetChar Function
[ ]

Returns 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

Tests for a null object
Obj Is Nothing
obj == null

Tests for a database null expression
IsDbNull
n/a

Threads primitives
SyncLock
lock










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.



[CodeEverywhere]
geovisit();

No comments: