tags:

views:

244

answers:

1

Recently in a previous project I came across a peculiar difference between VB.NET and C#.

Consider the following C# expression which:

null <= 2

This expression evaluates to False which is what I would expect. Then the corresponding VB.NET expression:

Nothing <= 2

I was surprised to learn that this expression actually evaluates to True

It seems like a fairly fundamental design decision between the two languages and it certainly caught me out.

Is anyone able to tell me why? Are null and Nothing one and the same? If so, why do they behave differently?

+24  A: 

Nothing in VB evaluates to the default value for a given type. (See this link for details.)

For an integer comparison (which the compiler will assume from the right hand operand), Nothing will thus be 0. 0 <= 2 is true for more obvious reasons :-)

Dan Puzey
It's also worth mentioning that Nothing is also used for null checks when combined with the Is and IsNot operators.
Justin Niessner
For completelyness: what does the compiler with the C# code? `(object)null` does not allow comparison with an integer, `Int32` can't be null ... ?
Stefan Steinegger
You beat me while I was looking for the answer. And you got the same link, too :P
Wayne Werner
@Stefan Steinegger: `int` can be implicitly converted to `int?`. See http://msdn.microsoft.com/en-us/library/2cf62fcy.aspx: "The conversion from an ordinary type to a nullable type, is implicit." Therefore the code `null <= 3` will result in the following warning: Comparing with null of type 'int?' always produces 'false'. I assume such code produced a compile time error in C# 2.0.
0xA3
@Wayne: first result from MSDN search - maybe my browser shortcut pipped you to the post ;-)
Dan Puzey
Sounds like the equivalent to `Nothing` in C# is `default(T)` where `T` is the type.
TMN
@Stefan/0xA3 - see my edit. I think that's what's going on with the C#.
Dan Puzey
@Dan Puzey: See Eric's answer here: http://stackoverflow.com/questions/1972262/c-okay-with-comparing-value-types-to-null/1972317#1972317. He is talking about the `==` operator but the same holds for the `<=` operator. It has nothing to do with the `CompareTo` overload.
0xA3
@Dan - yeah, I think it may have been the first result from a google search, too <edit> nope, it was 6th. But the first result pointing to an MSDN page
Wayne Werner
@0xA3: Done. Some of us have to work, you know ;-)
Dan Puzey