Skip to content

Choosing between C# and VB.NET

31 Mar 2005
tags: ,

The .NET Framework supports a variety of programming languages, including Microsoft’s much heralded C#. Huw Collingbourne considers whether Visual Basic is still as sharp as the competition.

Originally published on DNJ Online, March 2005

In the past, different programming languages tended to do things in their own way. Programmers using C++ would, in all probability, make use of the types and routines provided by the Microsoft Foundation Classes (MFC) libraries; Visual Basic had its own built-in types and routines;
non-Microsoft languages such as Delphi and Java used yet other class libraries, each of which was incompatible with each other.

With the advent of .NET that has changed. No matter which programming language you use, you will have access to the same rich collection of classes and functions provided by the .NET
Framework. Indeed, it is even possible to write classes in one language and derive descendant classes from them in another language. Your source code is not compiled directly into a machine code executable. Instead it is translated into Microsoft Intermediate Language (MSIL). This intermediate language is only converted into machine code when the program is run by the .NET Common Language Runtime (CLR).

In effect, the .NET CLR understands only one language: MSIL. This means that it really doesn’t matter which language your programs are written in. Whether they were written in C#, J#, VB, Delphi or some other .NET language, they will all end up as MSIL. Given the fact that all .NET programming languages have access to the same class library, are translated to the same intermediate language and are executed by the same runtime system, you may wonder what, if anything, there is to choose between them. In this article, I shall be taking a close look at Microsoft’s two principal .NET languages, C# and VB.NET, in an attempt to answer this question.

Choosing between C# and Visual Basic

As far as the .NET Framework is concerned, all programming languages are pretty much equal. In the past, you may have evaluated languages on the basis of their compilation and execution efficiency or their editing, design and debugging tools. Most of these considerations are irrelevant when programming for .NET. Languages such as C#, VB.NET and others can, in principle, access the same classes and output the same MSIL code. When hosted by Visual Studio .NET, they even share the same development environment.

The really important differences between the languages are not to be found in their interaction with the runtime system but in the support they offer to the programmer. In short, the syntax, the structure and the clarity of the languages are paramount.

You only have to glance at some C# and VB.NET code to see some of the obvious differences. On the whole, C# is much terser than VB.NET. To take a simple example, consider the way in
which the two languages delimit blocks of code. In common with other C-like languages such as C++ and Java, C# uses curly brace delimiters. VB.NET, by contrast, uses various keywords such as End If, End Sub and Next to terminate specific code blocks.

Some people would argue that the more verbose syntax of VB.NET makes the code clearer (more ‘self documenting’). On the other hand, it would equally well be argued that C# has the virtue of consistency, since curly braces are used to delimit everything and the built-in bracket matching tool makes it easy to find the braces at the start and end of each code block. Ultimately, this is a matter of personal preference which is likely to depend on your previous programming experience.

While it is generally true to characterise C# as the terser language, this is not invariably the case. When you need to read or write multiple properties of a single object, VB.NET provides the very useful with keyword. This lets you enter the object variable just once rather than repeatedly. This can often make VB.NET considerably less verbose than C#, as you can see in this example:

’ Set TabPage properties in VB.NET
 With TabControl1.TabPages(0)
 .BackColor = Color.Red
 .BorderStyle = BorderStyle.Fixed3D
 .ToolTipText = "Click Me!"
 .Text = "Hello world"
 End With

// Set TabPage properties in C#
 tabControl1.TabPages[0].BackColor = Color.Red;
 tabControl1.TabPages[0].BorderStyle = BorderStyle.Fixed3D;
 tabControl1.TabPages[0].ToolTipText = "Click Me!";
 tabControl1.TabPages[0].Text = "Hello world";

For programmers with no previous experience of a C-like language, VB.NET code will, in most cases, be easier to understand than the C# equivalent. Take the example of a simple for loop. You could read this VB.NET version aloud and understand what it does:

For i As Integer = 0 To 7
 TabControl1.TabPages(i).Text = "Tab: " + i.ToString()
 Next i

The same cannot be said of its C# equivalent:

for(int i = 0;i < 8; i++){
 tabControl1.TabPages[i].Text = "Tab: " + i;
 }

Incidentally, there are two interesting things to note in the code fragments shown above. First, experienced VB programmers may be surprised to see the declaration of i As Integer in the For loop. In previous versions of VB, the counter variable in a For loop had to be pre-declared. The other thing to note is that C# automatically converts the integer variable, i , to a string when it is concatenated using the + operator. VB.NET does not permit this. Instead it requires the variable to be explicitly converted using the ToString() method.

Smooth Operators

There are several non-intuitive or ambiguous features which C# has inherited from the C language. To take a simple example, C# allows programmers to increment or decrement the values of a variable by placing two plus ( ++ ) or minus ( ) characters either before or after the variable. The resulting value can then be assigned to some other variable. However, the effect of the assignment varies according to the position of the increment operators. If placed before the variable, the increment is done before the value is assigned. If placed after the variable, the value is assigned before the increment is done. Used carelessly, this can result in subtle bugs. The VB.NET syntax does not allow such ambiguity.

On the other hand, VB.NET has acquired some C-style operators which can be used for brevity. The += and -= operators, for instance, allow you to increment or decrement the value of a variable without having to repeat the variable name on the right-hand side of the assignment:

x += y    ’ this is the same as x = x + y

Other distinguishing features of the VB.NET and C# languages include the case sensitivity of C#. In VB.NET, a variable named MYVAR can also be written as myvar. In C#, these would be treated as two different variables. There are, of course, numerous syntactical differences ranging from the declaration of variables ( Dim
VariableName As TypeName in VB.NET, TypeName VariableName; in C#) to the way in which statements are terminated (typically the end of a line in VB.NET and a semi-colon in C#).

Better Safe Than Sorry?

More significantly, C# gives programmers the option of using pointers. Pointers are variables which refer or ‘point’ to specific locations in memory. They are so widely used in C and C++ that many programmers may find it difficult to conceive of programming without them.

The trouble with pointers is that they are inherently unsafe. They make it easy to corrupt memory, overwrite data and crash your applications. C# discourages the use of pointers but doe permit their use within blocks of code marked with the unsafe directive.

Code marked with the unsafe directive is ‘unmanaged’. This means that it is run as native machine code without benefiting from the services of the CLR. The use of pointers may be useful for certain special programming tasks that require the direct manipulation of memory or interaction with COM. In most cases, however, the .NET Framework provides all the services your program requires without having to resort to the hazards of unmanaged code. VB.NET is more restrictive than C# in this respect. It does not permit the use of unsafe code under any circumstances.

Even so, it is not quite true to say that pointers can never be used. VB.NET provides access to pointers using the IntPtr type. This allows a limited range of pointer operations. For example, an IntPtr variable can be used to store a handle to a file or a window.

If you prefer to program in VB.NET but have an occasional need to use pointers, you can, of course, add a C# or even a C++ project to your solution in order to do all the ‘dirty work’.

Developer Support

Aside from the features of the languages themselves, there are also several significant differences in the way in which they are supported by the Visual Studio .NET environment. The code editor provides more automatic formatting and IntelliSense code-completion for VB.NET than for C#. For example, to create a property in VB.NET you need only enter the keyword property and a name such as MyProp. When you press the Enter key, the editor generates all of the following code:

Property MyProp()
 Get
 End Get
 Set(ByVal Value)
 End Set
End Property

In C# you would have to enter all the equivalent code for a property by hand.

The editor supplies many other types of automatic formatting for VB.NET. For example, it adjusts the capitalisation of keywords for consistency and it optionally reformats code by logically aligning statements, adding missing end quotes to strings, supplying parentheses to function calls and so on. The automatic formatting options available to C# are far less complete.

Making the choice

In summary, when evaluating programming languages for .NET, the choice between C# and VB.NET is largely a matter of personal preference. In the past, VB may have been looked down upon by some developers who considered it to be inherently less powerful than other general purpose languages. But VB.NET is altogether a different beast from VB6. It is every bit as powerful as C#, it has full access to the .NET Framework and its compiled applications should generally be just as fast and efficient as similar applications written in C#.

The only major difference between the two languages is that C# can break out of the ‘managed’ world of .NET to support unsafe code should this be required. However unsafe code is, as its name suggests, inherently hazardous and you may feel that it is a good thing to avoid using it. Explicit use of pointers is seldom required when programming .NET. If you really feel that you cannot do without pointers, then C# would be a good choice of language. If you are happy to work within the managed world of .NET, then C# or VB.NET would be equally suitable for your purposes.

Of course, there are other .NET languages available too. At first sight, C++ might seem the most attractive choice for programmers with previous experience of that language. You need to be aware, however, that the .NET version of C++ is best used for manipulating unmanaged memory. In most cases, C# would be a better choice of .NET language for a programmer with C++ experience. Unlike C++, the C# language was specifically designed for the .NET Framework. It benefits from a simple syntax, garbage collection and type safety to eliminate many potential bugs.

For Java programmers, the J# language did provides a Java-style alternative to C#. It offered the easiest migration path for projects developed using Microsoft’s Visual J++. However, bear in mind that the syntax of C# is also very similar to that of Java. Moreover, both Java and C# provide garbage collection to reclaim unused memory, they both avoid pointers by default and
implement a simplified form of single-descent object orientation. Java programmers are best advised to choose the C# language.

Huw CollingbourneHuw Collingbourne has written columns and tutorials on Delphi, Java, VB, C++, C# and several more obscure languages for a variety of computer magazines. He has also written the Rants and Raves opinion column monthly since March 1988, first in Computer Shopper and later for PC Plus. This column now continues online. In addition he is co-founder of SapphireSteel Software.

3 Comments leave one →
  1. Simon permalink
    2 Feb 2012 19:29

    I’d point out that in the for loop, the conversion of i to a string should be moot – the + operator is not the best way to go – String.Format is superior in performance (immutable strings) and in formatting options. It’s also identical across VB/C#

    Like

  2. 30 Jan 2014 21:05

    Huh?

    > The other thing to note is that C# automatically converts the integer variable, i , to
    > a string when it is concatenated using the + operator. VB.NET does not permit this.

    Like

What do you think?