Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

C# Nullable Types…Subtlety

0.00/5 (No votes)
1 Apr 2009 1  
While moderating posts over on the Asp.Net forums, I ran into a thread containing questions about using Nullable types. Nullable types are a simple concept: Allow value types to have the value of null.

While moderating posts over on the Asp.Net forums, I ran into a thread containing questions about using Nullable types.

Nullable types are a simple concept: Allow value types to have the value of null. Typically, an integer or float cannot be null: When in scope, they always exist and therefore must have a numeric value (zero is not null, it is a numeric value).

Here is the subtlety (or in technical terms, a really ooky statement):

Nullable types with the value of null are not really null.

Look at this code:

C#
int Test1 = 0;  // standard value type
int? Test2 = null; // nullable value type
Object Test3 = null; // reference type

Response.Write("Test1: " + Test1.ToString() + "<br />");
Response.Write("Test2: " + Test2.ToString() + "<br />");
//Response.Write("Test3: " + Test3.ToString() + "<br />");

// Output:
//
// Test1: 0 // correct
// Test2: // no exception, what? but it's null!
//
// If Test3 is allowed to run, we get:
// "Object reference not set to an instance of an object."

It is odd that we can access Test2 when its value is null but we get no output string.

Microsoft's web site correctly describes nullable types (http://msdn.microsoft.com/en-us/library/1t3y8s4s.aspx):

"A nullable type can represent the correct range of values for its underlying value type, plus an additional null value."

But most of us gloss over descriptions like that and until you see it demonstrated, you may not truly understand.

I hope someone finds this useful, Steve Wellens.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here