Click here to Skip to main content
15,868,074 members
Articles / Programming Languages / C#

C# Nullable Types…Subtlety

Rate me:
Please Sign up or sign in to vote.
4.00/5 (2 votes)
1 Apr 2009CPOL 18.2K   4   4
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.

This article was originally posted at http://weblogs.asp.net/stevewellens/privaterss.aspx

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
EndWell Software, Inc.
United States United States
I am an independent contractor/consultant working in the Twin Cities area in Minnesota. I work in .Net, Asp.Net, C#, C++, XML, SQL, Windows Forms, HTML, CSS, etc., etc., etc.

Comments and Discussions

 
GeneralNullable Type in .NET Pin
elizas6-May-10 21:36
elizas6-May-10 21:36 
GeneralPoint worth mentioned Pin
Debashish Sarkar6-Jan-10 7:02
Debashish Sarkar6-Jan-10 7:02 
GeneralRe: Point worth mentioned Pin
Debashish Sarkar6-Jan-10 7:05
Debashish Sarkar6-Jan-10 7:05 
GeneralRe: Point worth mentioned Pin
Steve Wellens6-Jan-10 7:10
Steve Wellens6-Jan-10 7:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.