Sunday, February 12, 2012

C# or VB.NET -- aah the choices!

All Programming Fundamentals! Although there are many other supported languages in .NET Framework, when it comes to .NET development though, most applications are developed in either C# or VB.NET. I know, I know there is F# and what not. But the focus of this post and most of my other blogs would be on these two languages.

Gone are the days when C# was significantly better than VB.NET in terms of features etc. so most of the differences that remain now are superficial as well as preferential.

Still, I wanted to put down some of my thoughts as well as some of the arguments I have heard from either side.

So, which one is better? Well it depends on two factors...

  • Your own Experience / Preference.
  • Market Conditions.
For some reason, perhaps due to old VB 6 hangovers, C# developers are more in demand than VB.NET developers and I have seen more jobs in C# than VB.NET. Now, it may not be true in all markets but at least it is true on my side of the pond.

Still, there are a few subtle differences between the two and I will summarize them below, at least for the water cooler conversation, if nothing else. Note, there are syntantical differences like any language and we are not discussing them here.

  1. I don't know about you but I find it easier to read VB.NET code than C# code, due to its "English" like syntax. One developer once told me - "I don't understand why do I need to put a ; (semicolon) at the end of each line in C#."

  2. Line Continuation - If you want to continue one line into next, in VB.NET you have to do something like & _ while in C# you don't need to do anything. Just don't type ; (that's the answer I gave to the developer who asked me above question BTW). Although, in VB 2010 there is an implicit line continuation and you don't have to use _ anymore, but frankly if you are using VB.NET, I rather see  _.

  3. Previous versions of C# didn't support optional parameters to a method while VB.NET always did. Although latest C# release now supports optional parameters.

  4. Keywords in VB.NET are more self explanatory than C # (this goes to my #1 comment above). For example, to allow a method to be overridden in a derived class, you add a keyword "Overridable" to the method in VB.NET in the base class. In C#, you use keyword "Virtual" instead - not so intuitive, is it?

  5. Similarly if you don't want your method to be overriden, you use keyword "NotOverridable" in VB.NET but there is no such thing as "NotVirtual" in C#. Instead it is "Sealed".

  6. "With.... End With" in VB.NET has no equivalent in C#.NET.

  7. I personally feel that VB.NET IDE in Visual Studio has better intellisense than C# IDE.

  8. C# on the other hand is less forgiving if you make a mistake. For example, unless you have Option Strict ON, you can get away with converting a value type to a reference type or a string to an int etc. without typecasting it, which may impact the performance of your application or worst may result in run-time errors. C# will not let you convert one variable type to another without typecasting.

  9. If you have Option Explicit OFF, you can consume variables without declaring them in VB.NET, no such luck in C#.

  10. VB.NET is not case sensitive but C# is. I find that extremely annoying in VB.NET because I feel it is a poor coding practice to declare variable in one case and use it in another.

  11. VB.NET has much easier event handling. You can simply declare an event and raise it without having to implement any delegates. In C#, you have to use delegates.

  12. I find it much easier to read comments in C#. You use // to start the comment while ' in VB.NET. I like C# comment feature so much that even in VB.NET I use '// .

  13. It is much easier to port your skills from C# to Java or PHP as their syntaxes are somewhat similar. On the other hand VB syntax allows you to venture into other tools with VB Scripting support. For Example, SSRS Reports (you can create custom code in SSRS RDLC with VB.NET).
This is not an exhaustive list but the most common ones that I remember. If you feel I missed any, let me know and I will be glad to add to the list.

Your comments are welcome!

No comments:

Post a Comment