LANGUAGES: VB | C#
TECNOLOGIES: Development Languages
VB .NET vs. C#
Which to Choose in the Switch to .NET
By Craig Utley
With the release of .NET, Microsoft is creating two new languages, Visual Basic .NET and C#. Many developers are wondering which language they should choose, and current Visual Basic developers often say they re thinking about moving to C#. Does it really matter if you build your applications with VB .NET or C#? How do you choose between the two languages?
Why Do They Exist?
Before diving into their similarities and differences, it helps to understand why both languages exist and how they compare with their predecessors.
I often have the chance to ask people why Visual Basic exists. Microsoft s co-founder and chairman Bill Gates has a long history with the BASIC language. In fact, his first product was a BASIC interpreter for the Altair computer, written on punch tape. Microsoft s history with BASIC includes several versions of BASIC before Visual Basic, but Visual Basic was Microsoft s first real attempt to open Windows programming to the masses.
I have to admit that I scoffed when I first saw Visual Basic 1.0. I knew that real Windows programs were written in C++, not Visual Basic. Fortunately, however, I hadn t been too brainwashed by the C++ crowd, and, as I learned Visual Basic, I began to see a simple elegance in it. True, version 1.0 didn t exactly let you write mission-critical applications, but it was very easy to create a quick Windows application.
As Visual Basic evolved, it became much more powerful. Today, it is the most common way to build business applications on the Windows platform quickly. VB applications can be distributed, scalable, fast, and even mission-critical. Meanwhile, the C++ developers continue to regard VB as a toy language. After all, you can t really program without pointers (and pointers to pointers), can you?
While VB has gained a huge, worldwide following, the language (pre .NET) does have some missing elements. I rarely hear VB developers asking for pointers like those in C++, but they do ask for implementation inheritance, multithreading, and the ability to create Windows services and console applications natively. VB .NET answers all these requests and fixes many of the annoying little issues with VB. For example, you can instantiate variables at run time now, and parameter passing defaults to ByVal.
VB .NET is the modernization of VB, having added implementation inheritance and multithreading, but VB .NET has done so not so much through language changes as by supporting the features built into the .NET Framework. The Framework s Common Language Runtime, or CLR, provides inheritance and multithreading along with support for a variety of projects. VB .NET is one of the first-class languages on the .NET platform, meaning it fully supports the CLR and cannot be considered a second-class citizen to C#.
C# exists for a very different reason. While VB .NET added support for such things as multithreading and inheritance, C++ has had those capabilities for years. However, C++ often is used for plumbing. You don t typically write device drivers in VB. Instead, you use C++. If you take the concept of building a house, the C++ developers are the ones pouring the foundation, running the wiring, and installing the plumbing. VB developers then step in, put up the drywall, add carpeting, paint and furniture, and make the rooms functional. It s not that C++ can t do those things, it s just that it generally takes longer to do those things in C++. In business, time is money, and time is a luxury many businesses do not have.
Microsoft claims C# is the modernization of C++, just as VB .NET is the modernization of VB. While C# does try to eliminate some of the inconsistencies of the C++ language, it is really more about welcoming C++ developers into the world of building business applications quickly. In other words, C# allows C/C++ developers to finish those rooms and make them functional as fast as VB developers have been able to for years.
C# eliminates many of the areas that make C++ a minefield for developers. Because C# is built on the .NET Framework, it takes advantage of .NET s automatic memory management. This should eliminate many of the memory leaks often associated with incorrect use of memory in C++. C# also takes advantage of the CLR s type safety, preventing you from reading memory outside your object s memory range. And C# eliminates explicit pointers, which were the source of a tremendous number of bugs, especially for novice developers.
Some C++ developers see C# as the dumbing down of C++. Instead, it is an attempt to build a type-safe, reliable .NET language with syntax familiar to C++ developers. Similarly, VB .NET is an attempt to build a type-safe, reliable .NET language with syntax familiar to VB developers. The fact that VB .NET seems to gain a host of features is due more to its support of the CLR than to enhancements of the language. Because C++ already supported many of these features, some developers say the move to C# seems like a step backward.
VB .NET or C#: Does It Matter?
ASP classic developers have been debating VBScript vs. JScript on the server for about four years. The syntax of the two was obviously quite different, but, beyond that, the differences were minor. VBScript added a few nice objects, such as the Dictionary, the FileSystemObject, and the TextStream objects. But, overall, the languages let you accomplish the same thing.
The difference between the functionality of VB .NET and C# is even smaller. If you create a new project in Visual Studio .NET, you see the exact same project types listed for each language. So, if the project types are the same, and the functionality differences between the languages are small, does it matter which you choose?
If the answer was no, this would be a short article. The fact is that the language you choose probably won t hinge on the functionality in the language itself. Instead, your choice will hinge on some other criteria, which we ll examine in a moment.
What about Speed?
Unfortunately, it s too early to tell about speed. According to Microsoft, the speed between the two languages will be the same. Much of your code actually will be calling classes in the CLR, so that will be the same regardless of the language used to call it. As for the speed of items such as loops and string concatenation, that will depend on the compiler.
Microsoft officials say VB .NET and C# will compile to the same IL, so the result will be the same speed. I must admit I m skeptical, but there is no way to test this at this time. With .NET still in beta (.NET was not yet final when this article went to press. - Ed.), any speed tests most likely would bear little resemblance to the final product. For now, assume the speed will be very close, and don t worry about it until .NET is a finished product.
What about Language Differences?
The differences in the languages are small, but they do exist. For example, C# allows you to have unsigned integers of various sizes, and VB .NET does not. It is important to note, however, that unsigned integers are not part of the Common Language Specification (CLS), a part of the .NET Framework that defines what language elements can be used to allow inheritance between languages. If cross-language inheritance is something you want to ensure, you need to stick to only CLS-compliant types for any exposed parameters and return values.
VB .NET adds some of the C/C++ shortcuts, such as +=, -=, and others. Now, in VB .NET, x += 5 is the same as x = x + 5. However, VB .NET does not support the ++ or -- operators, which C/C++ programmers frequently use. C# supports all of these, of course. C# also supports the concept of operator overloading, in which a developer can overload operators such as +, - , and True. Operator overloading is admittedly not something most VB .NET developers will miss, but it is one of the differences between the two languages.
I won t go into the differences in the actual syntax here, except for two items that are only relevant to VB developers thinking of making the switch to C#: case sensitivity and the equality operator. (See Eric Smith s article There and Back Again for more on syntactical differences and how to move code from one language to another.) Don t forget that VB and VB .NET are not case sensitive, while C# is. Also, the assignment operator in C# is the equal sign (=) but the equality operator is the double equal sign (==). These two differences are a major headache for VB developers trying to make the switch to C#.
To see some of the general language differences, check out the sample VB .NET code in FIGURE 1.
Dim x As Integer = 5
Console.WriteLine("The secret number is: " & x)
FIGURE 1: Sample VB .NET code.
As you can see, VB .NET now allows you to initialize variable values at declaration. The next line uses the WriteLine method of the Console class. There is some simple string concatenation, and the string is printed to the console (or the DOS window or command window).
FIGURE 2 shows the same application written in C#. The variable declaration is cleaner, but the Console.WriteLine is almost identical. Notice the curly braces and semicolons in the C# program, which are familiar to C/C++ and Java developers.
static void Main(string args)
Console.WriteLine("The secret number is: " + x);
FIGURE 2: The same application as in FIGURE 1, but written in C# instead of VB .NET.
One of the differences when working with both languages in Visual Studio .NET is important to point out. When writing VB .NET, you can type console and when you press the period, the list of properties and methods appears. If you are typing in C# and you type console and press the period, nothing appears. This is because C# is case sensitive, and you ll have to type Console for the IntelliSense to work. VB .NET developers may find this tedious. Whether you consider it lazy or convenient, VB .NET developers typically type everything in lowercase and let the environment case it properly. C# developers have to case it properly as they type. This means C# forces a stricter set of code-writing standards, while VB .NET developers save wear and tear on the shift key.
So, how do you choose? If the functionality in the two languages is nearly the same, how do you go about choosing which language you want to use? I ll break this discussion into three categories: familiarity, resources and support, and personal edification.
The main reason to choose VB .NET or C# is familiarity, which is interesting because both languages are new. I ve had some people on the Microsoft project team tell me they don t want the move from VB to VB .NET to sound like a big move. Well, it is a big move. The learning curve is steep. The learning curve from VB to C# is steep. The learning curve from C++ to C# is steep.
You re learning a new language, but much of it is learning the .NET Framework and all the classes the CLR supplies. If you want to create a new thread, it s in the run time. If you want to access data using ADO.NET, it s in the run time. If you want to read in some XML, it s in the run time. I think you get the picture: Much of what you will be doing is learning the run time.
As you learn the run time, however, you ll be using some language to play with it. While VB .NET is a new language, and there are differences from VB that will cause you to lose your hair, I think it s safe to say that the learning curve going from VB to VB .NET is less steep than it is from VB to C#. C++ developers will feel more at home with C# than with VB. If you are a hard-core Java or JScript developer, you also will feel much more at home with C#.
VB developers will be much more at ease with VB .NET s loops, conditional constructs, variable declaration syntax, and more. Why fight case sensitivity and that double-equals sign when you don t have to? You ll find yourself searching for a bug for an hour, and it will turn out to be a missing equals sign. You don t need the headache of learning the nuances of a new language when you are trying to learn the CLR.
C++, Java, and JScript developers are used to case sensitivity, the double-equals, and all those curly braces. Moving to the more wordy VB .NET syntax would probably just be frustrating at a time when the developer should be focused on learning the CLR classes and not having to worry about an unfamiliar syntax.
Recommendation No. 1: Go with what s closest to you. If you are a new developer and have never written a line of code in your life, you ll have to find your answer in the next two sections.
Resources and Support
If you are learning a new language, what s the first thing you do? For most people, the first step is to create a Hello World program from a book or a help file. Some people actually read the language reference then. (Really, I ve seen people do it!) Most people go through online tutorials, maybe buy a book or two, and then look at the language reference only when they are stuck.
There will be no shortage of books, Web sites, and articles on either VB .NET or C#. So you should have plenty of opportunities to read about either language and have a number of examples from each language.
However, there are other factors to consider. Do you know anyone with experience in either one? Someone you can bounce ideas off of when you are having problems? What about industry support? Microsoft created both languages, but how many VB .NET developers will there be, compared with C# developers? Will Microsoft favor one over another in example code? Will your area offer training in both VB .NET and C#?
Most resources should be widely available in each language, but, if VB is any guide, there will be more VB .NET developers than there will be C# developers. While I expect the gap between the number of VB .NET and C# developers to be smaller than the gap between VB and C++ developers, there will likely be more VB .NET developers because so many VB developers will use VB .NET when they make the leap to .NET.
Perhaps you ve always wanted to program in C++ but found it too obscure, difficult, or overwhelming. Now, you see that C# exists but removes some of the more intensive tasks, such as managing memory and handling pointers. If it s been a lifelong dream of yours to program in C or C++, feel free to take C# for a spin. You ll find it quite forgiving and very powerful for creating business applications.
If you re thinking, Aha! Now I can learn C# and pull down the big bucks! then I need to warn you about something. You see, it s true in most cases today that C++ developers earn more than do VB developers. One reason is that there are a lot fewer C++ developers. Another reason, though, is the kind of work that C++ developers are doing. They are typically doing things you can t do (at least not reasonably) in VB. They re writing device drivers, socket interfaces, or any manner of low-level applications. VB developers, on the other hand, are busy solving business problems.
C# is not going to be used for the same purposes as C++. Instead, C# will be used for business applications. Therefore, C# and VB .NET will be used to create exactly the same kinds of applications. The advantage of one over the other will be miniscule when creating business applications. The only way you might earn more as a C# developer is that there likely will be fewer of them, so you may get lucky that way. It s unlikely, however, that you ll be building low-level applications. So, unless the manager is a dolt, you won t be worth more than a VB .NET developer.
Finally, pick the language you like. A friend tells me he has always thought C/C++ looked more elegant and logical to him than VB, but he s been using VB for a few years. He plans on making the jump to C# because it looks better. That s not a bad reason, but he ll be learning the CLR and C# at the same time. As a VB developer, he d probably have an easier time learning the CLR and VB .NET at the same time and then making the move to C#.
From a purely technical standpoint, which language you choose is probably irrelevant. Yes, there are some differences in the functionality between VB .NET and C#, but they re small. The main issue to consider is your comfort level with the languages on which these new languages are based: If you are familiar with VB, you ll have an easier (though not necessarily easy) transition to VB .NET. If you are a C, C++, Java, or JScript developer, you likely will find C# more comfortable.
Despite the two languages being different, they almost always will be used to produce the same sort of applications. Therefore, any salary difference between VB .NET and C# developers would be based on the sheer numbers of developers, and not on any specialized requirements of C# developers that VB .NET developers can t handle.
Most of you will use what your company tells you to use. If the choice is yours, feel free to choose either one. There s nothing wrong with learning both, but you should probably move to the one closest to your current language first, so you can learn the run time without having to fight the language too much. Then, any .NET language will just require you to learn some new syntax.
Craig Utley is a consultant, author, and trainer on Microsoft development tools and strategies. His book, A Programmer's Introduction to Visual Basic. NET, was given to all attendees at TechEd. Learn more about .NET by visiting Volant Training at http://www.volanttraining.com. Readers may contact Utley at mailto:[email protected].
Tell us what you think! Please send any comments about this article to [email protected]. Please include the article title and author.