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 / July 2006

Tip: Looking for answers? Try searching our database.

Reflection and Nullable

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Joe Bloggs - 13 Jul 2006 17:00 GMT
Hi,

Can someone please kindly show me how to determine if a type (read
value
type) is Nullable.

MSDN has this KB:
How to: Identify a Nullable Type (C# Programming Guide)
http://msdn2.microsoft.com/en-us/library/ms366789.aspx

however, using their code snippet, I couldn't get it to work:

public class MyClass
{
public static void Main()
{
 Nullable<int> j = new Nullable<int>(20);
 Type type = j.GetType();
 if (type.IsGenericType && type.GetGenericTypeDefinition() ==
typeof(Nullable<>))
 {
    Console.WriteLine("j is Nullable");
 }
 else
    Console.WriteLine("j is NOT Nullable");

  Console.WriteLine("Type of j is {0}", j.GetType());
}
}

The Output I got is:

j is NOT Nullable
Type of j is System.Int32

thus, is there  no way to determine a nullable or an non-nullable value

types?

Thanks for your help in advanced.
Barry Kelly - 13 Jul 2006 19:13 GMT
> Can someone please kindly show me how to determine if a type (read
> value
> type) is Nullable.

I answered in your multipost in dotnet.general.

-- Barry

Signature

http://barrkel.blogspot.com/

Jon Skeet [C# MVP] - 13 Jul 2006 19:23 GMT
> Can someone please kindly show me how to determine if a type (read
> value type) is Nullable.

Finding out if a type is nullable is fine, but you can't find out from
a value in the way you're doing. This is because calling GetType()
involves boxing the nullable value. Now, from the C# spec:

<quote>
A boxing conversion from a nullable type T? is processed as follows:

*      If the source value is null (HasValue property is false), the
result is a null reference of the target type.
*      Otherwise, the result is a reference to a boxed T produced by
unwrapping and boxing the source value.
</quote>

In other words, by the time you call GetType(), you're either calling
it on null (resulting in an exception) or on the boxed non-nullable
value type.

Now, do you actually need it from a Type object? If so, the test you
showed works fine. (Try it with typeof(Nullable<int>).)

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


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.