Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / September 2005

Tip: Looking for answers? Try searching our database.

Nullable types funkyness ?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Yannick Létourneau - 14 Sep 2005 17:21 GMT
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.
Bruno Jouhier - 14 Sep 2005 17:33 GMT
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" <YannickLtourneau@discussions.microsoft.com> a écrit
dans le message de news:
AB36297B-3764-4CFC-8ECB-746C4952868D@microsoft.com...
> Can someone explain to me why this code throws an exception ?
>
[quoted text clipped - 9 lines]
>
> Yannick L.
Yannick Létourneau - 14 Sep 2005 19:15 GMT
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.

> 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
[quoted text clipped - 23 lines]
> >
> > Yannick L.
Mattias Sjögren - 14 Sep 2005 21:09 GMT
>Do you have any pointers/links I could check to validate that this is indeed
>something being addressed by Microsoft ?

http://blogs.msdn.com/somasegar/archive/2005/08/11/450640.aspx

If you have access to the August CTP or September RC release of VS2005
you can verify it yourself.

Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.