Nullable types funkyness ?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Can someone explain to me why this code throws an exception ?

I mean, isn't that what Nullable types are supposed to be about ?

float? f = null;
Object o = f;
if (o != null)
throw new Exception("What the hell ?");


How can I test that an Object is null without doing some special cases for
Nullable types ?

Yannick L.
 
This is because nullable types are implemented as a hack with generics. So,
your nullable float is actually a "value" with a flag to indicate if it is
null or not. And when you assign it to an object variable, it gets boxed,
and the object is not null!

This is very bad (totally counter intuitive). Last I heard was that MS is
changing this and that nullable will be implemented differently in the
release. But this needs confirmation.

Bruno.

"Yannick Létourneau" <[email protected]> a écrit
dans le message de (e-mail address removed)...
 
Thanks for the reply. I totally agree with you.

Do you have any pointers/links I could check to validate that this is indeed
something being addressed by Microsoft ?

Thanks.
 
Back
Top