VB.NET and C# Comparison

Standard

Technically speaking VB.Net & C# are .Net family, using same base classes from .Net frame work, both are having own compiler and CLS (Common Language Specification) complaint. After compile into IL (byte code), it handled by the CLR (as virtual machine). Any CL specific language talks to each other in CLR. Assembly created in one .Net language able to reuse in software development using other .Net language. But syntactical representations are different.

If you have VB background (VB 4.0 or VB 5.0 or VB 6.0 or VB Script or VBA) VB.net syntax look so much familiar easy to learn, understand or pickup.  On the other hand if you have JAVA background (J2EE, java script) C# syntax look much familiar easy to learn, understand or pickup. Once familiar either vb.net or C# other CLS languages are much more easy to learn.

Syntax and Pseudo code comparison between VB.Net and C#

Description VB.Net C#
Single Comment line ‘Comment line

Rem Comment line

//Comment line
Multi Comment line ‘Comment line1

‘Comment line2

/* Comment line 1

Comment line 2 */

Syntax line terminator There is no such terminator ;
Incorporate assembly Import System using System;
End Statement Namespace, class, sub, function

Should close with corresponding end statement

NameSpace NS

Class cl

Sub Procedure

………….

End Sub

Function fn

…………..

End Function

End Class

End NameSpace

Namespace, class, methods should start and end with curly braces

 

nameSpace NS

{

class cl

{

……

}

}

Namespace Namespace < Name of Namespace >

End Namespace

namespace ns

{

}

Class <Modifier> class cl

……

End class

<Modifier> class cl

{

}

Access Modifier – Visible within the class Private private
Access Modifier – Visible within the class and derived classes. Protected protected
Access Modifier – Visible any classes within the namespace Friend internal
Access Modifier – Publically visible within the program. Public public
MustInherit abstract
NotInheritable sealed
  Overridable virtual
  overrides override
Variable declaration Dim <Name of variable> as <DataType>

Dim strName as string

<DataType> <Name of variable >

string strName ;

Array declaration Dim str() As String = New String() {“1”,   “2”, “3”}

Dim iNumbs(4) As Integer

Dim lNumb() As Long

ReDim lNumb(3)

string[] str = new string[]{“1”,   “2”, “3”};

int[] iNumbs = new int[4];

Array.Resize(ref array, size)

Keyword to inherit base class in the derived class declaration <modifier(s)> <Derived class name >

Inherits <Base class name >

(note that the Inherits keyword must appear on a new line)

Public MustInherit Class baseClass

 

Public Overridable Sub   testRo()

Console.WriteLine(“test”)

 

End Sub

End Class

Public Class derivedClass

Inherits   baseClass

Public Overrides Sub   testRo()

MyBase.testRo()

Console.WriteLine(“TEST”)

 

End Sub

End Class

<modifier> <Derived class name > : <Base class name >

 

 

 

 

public abstract class baseClass

{

public   virtual void  testRO()

{

Console.WriteLine(“test”);

}

}

public class derivedClass : baseClass

{

public   override void  testRO()

{

base.testRO();

Console.WriteLine(“TEST”);

}

}

Subroutine <Modifier(s)> Sub <Name of subroutine >(parameters)

…..

…..

 

End Sub

Public Overridable Sub testRo(ByVal name As String)

 

Console.WriteLine(“Hello ” + name)

End Sub

<Modifier(s)> void <Name of subroutine >(parameters)

{

….

….

}

public virtual void testRO(string   Name)

{

Console.WriteLine(“Hello ” + Name);

}

Function <Modifier(s)> Function <Name of function > (parameters) as  <Return DataType>

…..

…..

 

End Function

Public Function  testRo(ByVal   name As String)

Return “Hello ” + name

End Function

<Modifier(s)> <DataType> <Name of function > (parameters)

{

…..

…..

}

public string  testRO(string Name)

{

return “Hello ” + Name;

}

Parameter of subroutine or function Byval <ParameterName> as <Datatype>

Byref ParameterName as <DataType>

<DataType> <ParameterName>

 

ref <Datatype> <ParameterName>

Object properties With <Object>

.property 1

.property 2

End With

There is no similar syntax and feature in C#
For Loop Dim j As Integer = 0

For i As Integer = 0 To 9

j = j + I

Next

int j = 0;

for (int i = 0; i < 10;   i++)

{

j = i + j;

}

For Each Loop For Each n As String In vals

Next

foreach (string name in Name[])

{

—-

}

While Loop j as integer = 0;

i as   integer = 0 ;

While (j < 10)

i = i + j

If (j = 5) Then Continue While

j = +1

If (i = 5) Then Exit While

End While

int j = 0;

int i = 0 ;

while (j < 10)

{

i = i + j;

if (j == 5) continue;

j   =+ 1;

if (i == 5) break;

}

 Validate condition (decision)  with multiple choice  Select Case iCase

 

Is < 5

Console.WriteLine(“Value less then 5”)

 

 

 

 

 

 

Case 5,6 To 10

Console.WriteLine(“Value less then 10, but greater then or equal to 5”)

 

 

 

 

 

Case Else

Console.WriteLine(“Value is greater then 10”)

End Select

switch (i)

{

case 0:

case 1:

case 2:

case 3:

case 4:

Console.WriteLine(“Value is less then 5”);

break;

case 5:

case 6:

case 7:

case 8:

case 9:

Console.WriteLine(“Value is greater then 5 and less then 10”);

break;

default  :

Console.WriteLine(“Value is greater then 10”);

break;

}

 Validate condition with few choice (if statement)  If (i < 5) Then  

 

Console.WriteLine(“Value is less then 5”)

 

ElseIf (i < 10 And i >= 5) Then

 

Console.WriteLine(“Value is 5 or between 5 to 10”)

 

Else

 

Console.WriteLine(“Value is greater then 10”)

End If

if (i < 5)

{

Console.WriteLine(“Value is less then 5”)

}

else if ((i >= 5) && (i < 10))

{

Console.WriteLine(“Value is 5 or between 5 to 10”)

}

else

{

Console.WriteLine(“Value is greater then 10”)

}

 (Conditional) Ternary operator  IIF(<condition, true, false)  <condition>?true:false
 Error handling Try

 

 

Catch (Ex as Exception)

 

Finally

 

End Try

try

{

}

catch (exception ex)

{

}

finally

{

}

 Data type Difference  Except ‘integer‘ and ‘Single‘ data type;  For other data types both are using same keywords  Integer become int, Single become float
 Statement termination  No statement termination  ; is the statement termination
 Statement continuation  _ is statement continnuationn  No statemet continuation
 New line character  Vbcrlf  \r\n
 Tab character  Vbtab  \t
 Operator  Except

concodintion (&),

module (mod),

equal (=),

not equal (<>),

And also (AndAlso),

Or else (OrElse)

others look same

 

Concodination (+) ,

Module (%),

eual (==),

not equal (!=),

And Also (&&),

Or Else (||)

others look same

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s