VB vs. C#
June 12, 2012
The two big languages in .NET desktop development are VB.NET and C#. Both derive from long-lived and popular language families, and both have had their share of mutual dislike. Not dissimilar to the Capulets and the Montagues. C# has a proud history of hard-core development. It wasn’t that long ago that practically all serious desktop applications for Windows were written in C or C++. Visual Basic on the other hand has always been the softer language, aimed at non-professional programmers.
In the .NET framework the two languages have converged to an uncanny degree. The only real difference between them now is the ability to write unsafe code in C#, which you really oughtn’t be doing in the first place. But functionality isn’t everything. VB and C# were (are?) developed by different teams and they’ve come up with different approaches to similar problems. These differences have become fodder in the up-close war between Basic and C proponents, who now find themselves in uncomfortable proximity to the opposition.
Microsoft itself seems to be pushing C# harder than VB, for whatever reasons. But as my old* boss Andy leBihan pointed out, new programmers will always gravitate more towards VB, simply because it’s called Visual Basic, rather than C Sharp.
I’m not going to argue a position here, I’m happy working with both languages and I try to distribute new projects evenly between them. I started out as a VB programmer (VBScript, then VB5, then VB6, ultimately VB.NET 2005 → 2010) but have been working with C++ and C# for about 6 years now. There are plenty of things I think VB does better and plenty of other things that C# does better. Some of these are intrinsic to the language, others are mere implementation details of the VisualStudio code editor. Here follows a non-exhaustive list:
Things VB does better than C#:
- Events. Pretty much everything about them. Declaration, adding and removing handlers, raising events. Events are a bloody fucking nightmare in C#.
- Keywords. I like the self-documenting nature of VB language keywords. It makes them longer, but oh so much more readable. NotInheritable vs. sealed. MustOverride vs. abstract. Inheritable vs. virtual.
- End constructs. Pretty much all language constructs in C# are delineated by curly brackets. By the time you get to the bottom of a file, it’s not uncommon to see 10 closing brackets in a row. Good luck trying to find which ones mean what. VB uses keywords like End If, Next, Loop, End Function, End Class, End Namespace.
- Case-Insensitivity. I see no benefit whatsoever in having casing matter. All it can accomplish is confusion and it makes the autocomplete less easy to use.
- The Like operator. It’s just nice to have wild-card string comparison be so easily accessible.
- Interface implementations. I prefer the explicit statements for implementing interfaces as well as the suffix marking methods as specific implementations. C# is very muddy in this respect.
- Select Case is much easier to use than the C# switch construct.
Things C# does better than VB:
- It leaves scientific notation in code intact. There’s few things more annoying than having your 1e-12 literals automatically replaced with 0.000000000001
- Square brackets for indexing. VB uses round brackets for both method calls and array/list accessing. Square brackets would have been so much more distinguishable.
- Separate accessors for getters and setters. It’s not possible to have a public getter and a protected setter in VB. Forcing you to complicate the code with protected methods that kinda sorta look like the getters.
- Reflection Attributes. The notation is much cleaner in C#.
- /* … */ commenting. VB does not allow for comments inside lines of code or for multi-line comments.
- is and as keywords. Much easier to type and read than TypeOf and TryCast().
As mentioned, this is nowhere near a complete list, however these are my major gripes. Due to the absolute horrid nature of events in C#, I prefer VB while developing GUI intensive applications but for pure code libraries I think C# is the more efficient language, at least once you’ve mastered all the keyword incantations.
* ‘old’ as in ‘no more’, not as in ‘geriatric’