On CNET: Top 5 most popular products

VB.NET or C#: choose your weapon

Tags: .NET, Programming languages, Microsoft development tools, Tony Patton, C#, Microsoft Visual Basic.Net, End Sub, .NET Newsletter

  • Save
  • Print
  • Recommend
  • 14

Takeaway: The .NET Framework allows you to use your preferred language while offering most of the same features in VB.NET and C#. This article explores some of the subtle differences between VB.NET and C#.

The debate over which language to use—VB.NET or C#—has been going strong since .NET was first introduced. While you may use other languages, these two are by far the most popular. Each language uses the same .NET code base, so each is equally powerful for application development. Personally, I often go back-and-forth between the languages (for this column and customer projects). This week we'll take a closer look at the numerous language differences (mostly syntactical) 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.

Weekly .NET tips in your inbox
TechRepublic's free .NET newsletter, delivered each Wednesday, contains useful tips and coding examples on topics such as Web services, ASP.NET, ADO.NET, and Visual Studio .NET.
Automatically sign up today!

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.

Just the beginning

I've highlighted some of the common differences between C# and VB.NET. There are many more syntactical differences, but you'll discover those as you dig into the languages. Hopefully, the anomalies pointed out will prove fruitful if you find yourself transitioning to the other language.

Miss a column?

Check out the .NET Archive, and catch up on the most recent editions of Tony Patton's column.

Tony Patton began his professional career as an application developer earning Java, VB, Lotus, and XML certifications to bolster his knowledge.

  • Save
  • Print
  • Recommend
  • 14

Print/View all Posts Comments on this article

Table headings wrong way round!mattcol  | 05/24/06
Worked in both. Another difference with arraysMark Miller  | 05/24/06
Good catchaspatton@...  | 05/24/06
Well that was...Robsta  | 10/03/07
Having worked in bothBillT174  | 05/24/06
obscure fashionmarcus@...  | 05/24/06
Do Java and C# have the same syntax?Tobe-Soft  | 05/24/06
Differences between Java and C#Mark Miller  | 05/30/06
Update Your Java Knowledgechuckheaton@...  | 06/01/06
Hmmmm....roberw  | 06/01/06
Java ignoranceMark Miller  | 06/03/06
Sorry - I did not mean to flamechuckheaton@...  | 06/05/06
Kudos on the apologyithelp48@...  | 11/05/08
Delphi becomes C#polinastya-techRepublic@...  | 06/03/06
Starting with Java 1.5 java has Enum built-injean-simon.s.larochelle@...  | 10/03/07
Generics.GoDaves  | 10/04/07
Thanks for the inputs...Tobe-Soft  | 06/05/06
MCSDlmcfarlin  | 10/04/07
The Pointing out the difference is good...sundaramwithcs@...  | 10/03/07
Not necessarily truerwweiman@...  | 05/25/06
Cool...Tobe-Soft  | 05/25/06
Myself also.GoDaves  | 10/04/07
length variable in C# and VBrkrawitz@...  | 05/24/06
At the risk ofalaniane@...  | 10/04/07
great books on exposing differencesarzewski@...  | 05/24/06
MSDN article on differencesElvis.Is.Alive  | 05/24/06
My Favorite Languagera.kish  | 05/24/06
I used to like ANSI CToo Old For IT  | 05/24/06
If I had to pick a personal favoritealaniane@...  | 10/04/07
Comments on points mentioned in the articleonbliss  | 05/24/06
Some functions only exist in one of the languagesBob G Beechey  | 05/27/06
There's also "with"Mark Miller  | 05/30/06
Financial functions are missing in C#The Terminated  | 06/06/06
The two languages are differentMark Miller  | 06/06/06
And Yet...ra.kish  | 06/07/06
Yes, they're supersetsMark Miller  | 06/08/06
Re: Financial functions have nothing to do with a languagearzewski@...  | 10/03/07
Programming is about doing a jobsherman.meeds@...  | 10/03/07
You can Add the VB Rerferencesalan williams  | 09/12/07
Have We Become so Jaded?ra.kish  | 06/07/06
Make your caseShaunny Boy  | 10/03/07
RE: VB.NET or Cmathivanan.duraimanickam@...  | 10/03/07
VB.NET does support unsigned integersfcarr@...  | 10/03/07
I have yetalaniane@...  | 10/04/07
RE: VB.NET Collections - How is it done in C#?gregory.rubinstein@...  | 10/03/07
Generics usuallyTony Hopkinson  | 10/03/07
any examples? sample code?gregory.rubinstein@...  | 10/04/07
Depends on how far you want to goTony Hopkinson  | 10/05/07
Re: Depends on how far you want to gogregory.rubinstein@...  | 10/05/07
Now you've lost meTony Hopkinson  | 10/05/07
It depends on what you want to do and wheresherman.meeds@...  | 10/03/07
I always read it asTony Hopkinson  | 10/03/07
RE: VB.NET or Calaniane@...  | 10/03/07
RE: VB.NET or CTWB3  | 11/02/07
RE: VB.NET or Cchandoosgroup@...  | 01/21/08
RE: VB.NET or Ctyyp999@...  | 08/14/08

What do you think?

Article Categories

Security
Security Solutions, IT Locksmith
Networking and Communications
E-mail Administration NetNote, Cisco Routers and Switches
CIO and IT Management
Project Management, CIO Issues, Strategies that Scale
Desktops, Laptops & OS
Windows 2000 Professional, Microsoft Word, Microsoft Excel, Microsoft Access, Windows XP,
Data Management
Oracle, SQL Server
Servers
Windows NT, Linux NetNote, Windows Server 2003
Career Development
Geek Trivia
Software/Web Development
Web Development Zone, Visual Basic, .NET
advertisement
Click Here