Oh my god, VB.NET idiosyncrasies…
While I was catching up on my blog roll, I’ve noticed that my friend Alberto ended up being surprised by another nitpick difference between C# and VB.NET which does not help the programmer which needs to be proficient in both languages. Here’s the culprit code (in VB.NET and then in C#):
Dim x as Integer = 5 / 20 * 100
int x = 5 / 20 * 100;
At first, they do look the same, don’t they? But nop, they’re not the same. Unfortunately, VB.NET has 2 operators for division: / and . In practice, / will run the division and return a float (don’t ask me why!), while returns the integer result (discarding the remainder).
Now, I really don’t want to start bad mouthing VB.NET, but in this case, we’re dividing two integers, so I’d expect to get an integer. Now, this probably won’t make sense to the long time VB.NET programmer, but the truth is that C# will only allow you to perform three “division types”: integer, floating point and decimal.
I really don’t agree with my friend Alberto, which classifies C#’s behavior as wrong. In fact, I think it’s correct and it does help to achieve type safety. but hey, that’s me, a C# lover (and also a VB.NET critic )