C# vs. VB.Net
Recently, a few of my co-workers have posed the question of which is the "better" .Net language. So, I went out to look and see if there was any advantage to coding in either C# or VB.Net, just to clear the air.
I was under the (mistaken) impression that C# had a slight performance advantage over VB.Net. The bottom line is that they both compile into the same MSIL (Microsoft Intermediate Language) code, so it really doesn’t matter. In fact, you could use J# or Visual C++ if you wanted to, and they would perform just as well. As they both (C# and VB.Net) compile the same, they both use the same level of “Generational” garbage collection. Ultimately, there is no performance advantage to using C# over VB.Net.
It would seem that the differences between the two are mainly superficial, but there are some technical differences between them. C# gives you access to pointers or the ability to run what is known as “unsafe” code. VB.Net doesn’t allow you to do this, with the argument that direct access to memory is something that you shouldn’t have to worry about in a .Net world. If you do have that need, C# gives you that option. But if you make a mistake in doing so, you will be left with trying to track-down some very hard-to-find errors. After all, there’s a reason they call it “unsafe” code.
C# takes away some of the nicer syntax of VB.Net (like the “with” construct) and it requires all function calls to have a parameter list (as in "ToString()" instead of "ToString"). In C# you cannot resize an array, whereas in VB.Net you would simply do a “redim preserve”. In C#, you would first need to create a new array of the desired size, copy the old array data to the new array, and then destroy the old array. Essentially, one LOC (lines of code) becomes three. These may seem like points against C#, but what it does is make the developer more-accountable for how their code works.
VB.Net has received some criticism in that it supports poor programming practices. Variable names are not case-sensitive and implicit type conversion (“option strict off”) is enabled by default. Additionally, many VB.Net critics note that VB makes it easier for a developer to “slip backwards” into procedural programming. Throw-in a “goto” statement or two and suddenly you’ve produced something that looks like bad COBOL.
And by the way, both C# and VB.Net still allow the use of the “goto” statement. This is a downfall of both languages, in my opinion. Computer Science pioneer Edsger Dijkstra identified the “goto” statement as “harmful” in an ACM newsletter in 1968. Languages have evolved enough over the last forty-two years, so that nobody should need to use it. As Dijkstra said, “it is too much an invitation to make a mess of one's program.”
Personally, when given the option, I will always choose C# for new development. I’ve done this ever since 2008. Here are my reasons why:
- I came through UW-Whitewater’s MCS program when C++ was still the mainstay of their curriculum. For the first 3 years of my career, I coded in C. For me, I feel more-comfortable coding in a C-based language.
- C# offers consistent, terse construct delimiters (curly braces). For VB.Net, I have to type-out things like “end-if”, “end-while”, and “next”.
- C# requires each line to end with a semi-colon, making it easier to see each individual EOL (end of line).
- C# has additional operators (++/--) that make incrementing counters and indexes easier.
- When I need to find a .Net example via Google, C# is the most-common language used for demonstration purposes (as I’m sure you all have noticed). Sure, there’s a lot out there done in both. But if an example is done in only one language, it will most-likely be in C#.
In conclusion, choosing between C# and VB.Net really comes down to personal preference. While neither presents a major technical advantage I would pose the argument that it is easier to write bad code in VB.Net.
One final point to consider is that most ecommerce solutions are driven by a C-based language (probably Java). Going from C# to Java is an easier transition than going from VB.Net, so I encourage all of you (VB developers) to give C# a try. You can never hurt yourself by learning something new.
Huw Collingburne (Mar 2005), Choosing Between C# and VB.NET, http://www.dnjonline.com/article.aspx?ID=mar05_vbvscsharp
DMB Consulting, LLC (2008), What Language Should You Use? VB.Net or C#, http://www.dmbcllc.com/Articles/WebDevelopment/VBNETorCSharp/tabid/164/Default.aspx
Edsger Dijkstra (Mar 1968), Go To Statement Considered Harmful, http://www.u.arizona.edu/~rubinson/copyright_violations/Go_To_Considered_Harmful.html
TIOBE Software (Jul 2010), Tiobe Programming Community Index for July 2010, http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html
Note: This has been edited to remove any specific mention of my job or employer, revealing or otherwise. The opinions expressed within are mine alone.