For a very long time, I coded in VB as my primary language. I've made the transition to C#, and I'm very glad I did. When I ported some VB.NET code to C#, I was alarmed by some of the things that VB.NET's compiler would let me get away with that I wasn't even really aware that I was doing.
Here are a few examples (kindly ignore the crappy code, since it's purely for educational purposes):
Imports System.Diagnostics
Dim log As New EventLog("Application", Environment.MachineName, "MyApp")
log.WriteEntry("MyApp", "Message Text", EventLogEntryType.Information)
The C# compiler balks at this whereas VB just smiles and nods. The problem is that I'm invoking a static method (WriteEntry) on an instance variable.
Then there's this:
If CInt(txtQuantity.Text) Then
' Do something spectactular
End If
Visual Basic helpfully converts the result of CInt to a Boolean to help evaluate the If...Then statement. If it’s nonzero, you get True and something spectacular happens. In C#, you get a lovely compiler error about not being able to cast an int to a bool. Why? Because an int isn’t a bool, stupid!
In Visual Basic, you can do this for days and the compiler will pat you on the back while you do it:
Public Function FooBar() As Integer
Dim result As Integer
Return result
REM Do some more real work--that's unreachable
Return result
End Function
Does the compiler care? Nope. Not a peep. C#, on the other hand, gives you this nifty warning: “Unreachable code detected.” Then it gives you the file name and line number where it’s at. It’s like your best friend saying, “Hey man, you really don’t want to do that.”
What I found was that I was doing a lot of things that were just plain sloppy; things that I'd been doing for a long time and not even thinking about it. C# enforces better habits. It makes me think about what I'm doing, which naturally results in fewer defects. So I vastly prefer it over VB.