Nullable types funkyness ?

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.
 
B

Bruno Jouhier

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)...
 
G

Guest

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top