Google

Wednesday, October 3, 2007

Some difference between VB.Net and C#

Make your case

In my opinion, the biggest difference (by far) between VB.NET and C# is the fact that C# is case-sensitive. This can be a problem for former Visual Basic or current VB.NETers to grasp as they move to C#. After all, the following is legal in C#:

stringfirstName;

string FirstName;

string Firstname;

These three variables are unique with no problems, but the same (or similar) VB.NET code will not compile since each variable is seen as having the same name:

Dim firstName As String

dim FirstName As String

Dim Firstname As string

Other languages like JavaScript and Java are case-sensitive, so this isn't an anomaly—rather developers may view VB.NET's lack of case-sensitive support as out-of-the-ordinary.

Delimiters

Another major difference in the languages is line termination and code blocks. VB.NET uses a carriage return at the end of each line, so lines of code are contained on their own line. On the other hand, C# code lines use the semi-colon at the end of each line of code. Each line of code does not need to be on its own line, but it must end with a semi-colon. If you forget a semi-colon, you'll encounter a code error.

A block of code is a collection of statements. Examples include a class, its methods, loops, and so forth. C# marks a code block with curly braces (the starting brace { and the ending brace }) while VB.NET ends code blocks with statements (such as End If, Next, and End Sub). The following C# code snippet declares a class with a constructor with the curly braces defining the groups:

public class TestClass {

public TestClass() {

// Constructor

}

~TestClass() {

// Destructor

}

public void DoSomething() { }

public intDoSomethingElse() {}

}

And, VB.NET uses end statements to delineate a code block:

Class TestClass

Public Sub New()

' Constructor

End Sub

Protected Overrides Sub Finalize()

REM Destructor

End Sub

Public Sub DoSomething()

End Sub

Public Function DoSomethingElse() As Integer

End Function

End Class

These code snippets demonstrate the approach to blocks of code—most notably, a class declared with two methods. Actually, the code demonstrates a few other points. Each class declares constructor and destructor methods as well as two methods. The first method (DoSomething) does not return a value, while the second method (DoSomethingElse) does. You'll notice that VB.NET uses Sub for methods that return no values and Function for its counterpart. C# uses void for no return value and specifies the return value otherwise. The code also uses comments.

The C# comments are single-line with the double backslashes (//); multi-line comments may be used as well—they are enclosed as a group between backslash and asterisk combinations (/* and */). VB.NET only supports single-line comments denoted with either the apostrophe (') or REM statement.

Data types

The two languages support the same data types with a few differences in syntax. The following table provides an overview.

VB.NET


C#

Boolean


bool

Byte


byte, sbyte

Char


char

Short, Integer, Long


short, ushort, int, unit, long, ulong

Single, Double


float, double

Decimal


decimal

Date


DateTime

Object


object

String


string

You may notice a big difference where C# supports unsigned values while VB.NET does not. Variable declaration presents differences as well. VB.NET uses the dim statement to declare (or dimension) a variable, and the as keyword is used to assign a type as the following lines demonstrate:

Dim i As Integer

Dim first As String

On the other hand, C# starts with the type followed by the name:

int I;

string first;

When working with variables and objects, you will often work with arrays where another syntax difference arises.

Brackets

One of the language differences that always seem to trip me up is the use of brackets by C# for accessing individual elements of an array or collection of values. On the flip side, VB.NET uses parentheses in the same situations. The following code declares and populates an array of integer values:

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

for (int x=0; x < test.Length; x++) { Console.WriteLine(test[x]); };

Here's the equivalent VB.NET code:

Dim test() As Integer = {1, 2, 3, 4, 5}

For i As Integer = 0 To (test.Length - 1)
Console.WriteLine(test(i))

Next i

The code demonstrates the different syntax of the for loop as well.

Equality

The final discrepancy between the languages that I'd like to cover is the syntax for determining the equality of two values. This is a common misstep for developers new to C# (and JavaScript coders as well), as C# uses the double equals sign (==) to test for equality, while VB.NET uses the single equals sign (=). The following code samples demonstrate testing equality—beginning with C# that determines if an integer is equal to a value and displays text accordingly:

if (i == 2) { Console.WriteLine("They are equal"); }

else { Console.WriteLine("They are not equal"); }

Here's the VB.NET equivalent:

Dim i As Integer

If (i = 2) Then
Console.WriteLine("They are equal")

Else
Console.WriteLine("They are not equal")

End If

The frustrating aspect of this difference is it is not caught during compilation, so using the single equals sign in C# will assign the value instead of determining equality and thus logic problems that requires debugging.

No comments: