VB .NET vs. C#

Which to Choose in the Switch to .NET

Craig Utley

October 30, 2009

15 Min Read
ITPro Today logo

asp:coverstory

LANGUAGES: VB| C#

TECNOLOGIES: DevelopmentLanguages

 

VB .NETvs. C#

Whichto Choose in the Switch to .NET

 

By CraigUtley

 

With the release of .NET, Microsoft is creating two newlanguages, Visual Basic .NET and C#. Many developers are wondering whichlanguage they should choose, and current Visual Basic developers often saythey re thinking about moving to C#. Does it really matter if you build yourapplications with VB .NET or C#? How do you choose between the two languages?

 

Why Do TheyExist?

Before diving into their similarities and differences, ithelps to understand why both languages exist and how they compare with theirpredecessors.

 

I often have the chance to ask people why Visual Basicexists. Microsoft s co-founder and chairman Bill Gates has a long history withthe BASIC language. In fact, his first product was a BASIC interpreter for theAltair computer, written on punch tape. Microsoft s history with BASIC includesseveral versions of BASIC before Visual Basic, but Visual Basic was Microsoft sfirst real attempt to open Windows programming to the masses.

 

I have to admit that I scoffed when I first saw VisualBasic 1.0. I knew that real Windows programs were written in C++, notVisual 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 Windowsplatform quickly. VB applications can be distributed, scalable, fast, and evenmission-critical. Meanwhile, the C++ developers continue to regard VB as a toylanguage. After all, you can t really program without pointers (and pointers topointers), can you?

 

While VB has gained a huge, worldwide following, thelanguage (pre .NET) does have some missing elements. I rarely hear VB developersasking for pointers like those in C++, but they do ask for implementationinheritance, multithreading, and the ability to create Windows services andconsole applications natively. VB .NET answers all these requests and fixesmany of the annoying little issues with VB. For example, you can instantiatevariables at run time now, and parameter passing defaults to ByVal.

 

VB .NET is the modernization of VB, having addedimplementation inheritance and multithreading, but VB .NET has done so not somuch through language changes as by supporting the features built into the .NETFramework. The Framework s Common Language Runtime, or CLR, providesinheritance 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 fullysupports the CLR and cannot be considered a second-class citizen to C#.

 

C# exists for a very different reason. While VB .NET addedsupport for such things as multithreading and inheritance, C++ has had thosecapabilities for years. However, C++ often is used for plumbing. You don ttypically write device drivers in VB. Instead, you use C++. If you take theconcept of building a house, the C++ developers are the ones pouring thefoundation, running the wiring, and installing the plumbing. VB developers thenstep in, put up the drywall, add carpeting, paint and furniture, and make therooms functional. It s not that C++ can t do those things, it s just that itgenerally 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 asVB .NET is the modernization of VB. While C# does try to eliminate some of theinconsistencies of the C++ language, it is really more about welcoming C++developers into the world of building business applications quickly. In otherwords, C# allows C/C++ developers to finish those rooms and make themfunctional as fast as VB developers have been able to for years.

 

C# eliminates many of the areas that make C++ a minefieldfor developers. Because C# is built on the .NET Framework, it takes advantageof .NET s automatic memory management. This should eliminate many of the memoryleaks often associated with incorrect use of memory in C++. C# also takesadvantage of the CLR s type safety, preventing you from reading memory outsideyour object s memory range. And C# eliminates explicit pointers, which were thesource 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 withsyntax familiar to C++ developers. Similarly, VB .NET is an attempt to build atype-safe, reliable .NET language with syntax familiar to VB developers. Thefact that VB .NET seems to gain a host of features is due more to its supportof the CLR than to enhancements of the language. Because C++ already supportedmany of these features, some developers say the move to C# seems like a stepbackward.

 

VB .NET orC#: 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 obviouslyquite different, but, beyond that, the differences were minor. VBScript added afew nice objects, such as the Dictionary, the FileSystemObject,and the TextStream objects. But, overall, the languages let youaccomplish 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 theexact same project types listed for each language. So, if the project types arethe 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. Thefact is that the language you choose probably won t hinge on the functionalityin the language itself. Instead, your choice will hinge on some other criteria,which we ll examine in a moment.

 

What aboutSpeed?

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 bethe same regardless of the language used to call it. As for the speed of itemssuch as loops and string concatenation, that will depend on the compiler.

 

Microsoft officials say VB .NET and C# will compile to thesame IL, so the result will be the same speed. I must admit I m skeptical, butthere is no way to test this at this time. With .NET still in beta (.NET was not yet final when this articlewent to press. - Ed.), any speed tests most likely would bear littleresemblance 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 aboutLanguage Differences?

The differences in the languages are small, but they doexist. 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 integersare not part of the Common Language Specification (CLS), a part of the .NETFramework that defines what language elements can be used to allow inheritancebetween languages. If cross-language inheritance is something you want toensure, you need to stick to only CLS-compliant types for any exposedparameters and return values.

 

VB .NET adds some of the C/C++ shortcuts, such as +=,-=, and others. Now, in VB .NET, x += 5 is the sameas x = x + 5. However, VB .NET does notsupport the ++ or -- operators, which C/C++ programmersfrequently use. C# supports all of these, of course. C# also supports theconcept of operator overloading, in which a developer can overload operatorssuch as +, - , and True. Operator overloading isadmittedly not something most VB .NET developers will miss, but it is one ofthe 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 makingthe switch to C#: case sensitivity and the equality operator. (See EricSmith s article Thereand Back Again for more on syntactical differences and how to move codefrom one language to another.) Don t forget that VB and VB .NET are notcase sensitive, while C# is. Also, the assignment operator in C# is the equalsign (=) but the equality operator is the double equal sign (==).These two differences are a major headache for VB developers trying to make theswitch to C#.

 

To see some of the general language differences, check outthe sample VB .NET code in FIGURE 1.

 

Module Module1

 

   Sub Main()

      Dim x As Integer = 5

      Console.WriteLine("The secret numberis: " & x)

   End Sub

 

End Module

FIGURE 1: Sample VB .NET code.

 

As you can see, VB .NET now allows you to initializevariable values at declaration. The next line uses the WriteLine methodof the Console class. There is some simple string concatenation, and thestring is printed to the console (or the DOS window or command window).

 

FIGURE 2 shows the same application written in C#. Thevariable declaration is cleaner, but the Console.WriteLine is almostidentical. Notice the curly braces and semicolons in the C# program, which arefamiliar to C/C++ and Java developers.

 

using System;

 

namespaceConsoleApplication3

{

  class Class1

  {

    static void Main(string[] args)

    {

      int x=5;

      Console.WriteLine("The secretnumber is: " + x);

    }

  }

}

FIGURE 2: The same application as inFIGURE 1, but written inC# instead of VB .NET.

 

One of the differences when working with both languages inVisual Studio .NET is important to point out. When writing VB .NET, you cantype console and when you press the period, the list of properties andmethods appears. If you are typing in C# and you type console and press theperiod, nothing appears. This is because C# is case sensitive, and you ll haveto type Console for the IntelliSense to work. VB .NET developers may findthis tedious. Whether you consider it lazy or convenient, VB .NET developerstypically type everything in lowercase and let the environment case itproperly. 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 savewear and tear on the shift key.

 

So, how do you choose? If the functionality in the twolanguages is nearly the same, how do you go about choosing which language youwant to use? I ll break this discussion into three categories: familiarity,resources and support, and personal edification.

 

Familiarity

The main reason to choose VB .NET or C# is familiarity,which is interesting because both languages are new. I ve had some people onthe Microsoft project team tell me they don t want the move from VB to VB .NETto 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# issteep.

 

You re learning a new language, but much of it is learningthe .NET Framework and all the classes the CLR supplies. If you want to createa 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. Ithink you get the picture: Much of what you will be doing is learning the runtime.

 

As you learn the run time, however, you ll be using somelanguage to play with it. While VB .NET is a new language, and there aredifferences from VB that will cause you to lose your hair, I think it s safe tosay that the learning curve going from VB to VB .NET is less steep than it isfrom VB to C#. C++ developers will feel more at home with C# than with VB. Ifyou are a hard-core Java or JScript developer, you also will feel much more athome with C#.

 

VB developers will be much more at ease with VB .NET sloops, conditional constructs, variable declaration syntax, and more. Why fightcase sensitivity and that double-equals sign when you don t have to? You llfind yourself searching for a bug for an hour, and it will turn out to be amissing equals sign. You don t need the headache of learning the nuances of anew language when you are trying to learn the CLR.

 

C++, Java, and JScript developers are used to casesensitivity, the double-equals, and all those curly braces. Moving to the morewordy VB .NET syntax would probably just be frustrating at a time when thedeveloper should be focused on learning the CLR classes and not having to worryabout an unfamiliar syntax.

 

Recommendation No. 1: Go with what s closest to you. Ifyou 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.

 

Resourcesand Support

If you are learning a new language, what s the first thingyou do? For most people, the first step is to create a Hello World program froma 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 theyare stuck.

 

There will be no shortage of books, Web sites, andarticles on either VB .NET or C#. So you should have plenty of opportunities toread about either language and have a number of examples from each language.

 

However, there are other factors to consider. Do you knowanyone with experience in either one? Someone you can bounce ideas off of whenyou are having problems? What about industry support? Microsoft created bothlanguages, but how many VB .NET developers will there be, compared with C#developers? Will Microsoft favor one over another in example code? Will yourarea offer training in both VB .NET and C#?

 

Most resources should be widely available in eachlanguage, but, if VB is any guide, there will be more VB .NET developers thanthere 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 VBdevelopers will use VB .NET when they make the leap to .NET.

 

PersonalEdification

Perhaps you ve always wanted to program in C++ but foundit too obscure, difficult, or overwhelming. Now, you see that C# exists butremoves some of the more intensive tasks, such as managing memory and handlingpointers. If it s been a lifelong dream of yours to program in C or C++, feelfree to take C# for a spin. You ll find it quite forgiving and very powerfulfor creating business applications.

 

If you re thinking, Aha! Now I can learn C# and pull downthe big bucks! then I need to warn you about something. You see, it s true inmost cases today that C++ developers earn more than do VB developers. Onereason is that there are a lot fewer C++ developers. Another reason, though, isthe kind of work that C++ developers are doing. They are typically doing thingsyou 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, onthe 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 .NETwill be used to create exactly the same kinds of applications. The advantage ofone over the other will be miniscule when creating business applications. Theonly way you might earn more as a C# developer is that there likely will befewer of them, so you may get lucky that way. It s unlikely, however, thatyou 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 hehas always thought C/C++ looked more elegant and logical to him than VB, buthe 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 andC# at the same time. As a VB developer, he d probably have an easier timelearning the CLR and VB .NET at the same time and then making the move to C#.

 

Conclusion

From a purely technical standpoint, which language youchoose is probably irrelevant. Yes, there are some differences in thefunctionality between VB .NET and C#, but they re small. The main issue toconsider is your comfort level with the languages on which these new languagesare based: If you are familiar with VB, you ll have an easier (though notnecessarily easy) transition to VB .NET. If you are a C, C++, Java, or JScriptdeveloper, you likely will find C# more comfortable.

 

Despite the two languages being different, they almostalways will be used to produce the same sort of applications. Therefore, anysalary difference between VB .NET and C# developers would be based on the sheernumbers of developers, and not on any specialized requirements of C# developersthat 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 wrongwith learning both, but you should probably move to the one closest to yourcurrent language first, so you can learn the run time without having to fightthe language too much. Then, any .NET language will just require you to learnsome new syntax.

 

Craig Utley is a consultant, author, and trainer on Microsoftdevelopment 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 VolantTraining at http://www.volanttraining.com.Readers may contact Utley at mailto:[email protected].

 

Tell us what you think! Please send any comments about thisarticle to [email protected].Please include the article title and author.

 

 

 

Read more about:

Microsoft
Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like