Nullable Types

Published Tue, Feb 28 2006 19:08 | Paul June

Well, this is the official opening of my blog.
I’ll be blogging on the new features of Visual C# 2005 on my first few posts. The newest feats that I love on C# is on its new Nullable Types.

Nullable types gives you the ability to create types that can hold null values. You might be wondering on how would this be useful. Well nullable types can be very useful when dealing with database values. Such as you don’t need to convert null values in your database to a blank string or 0 if numeric when passing it directly to a variable.

By default, types cannot contain null values. Here’s an example that demonstrates this:

int x = null;

The code above generates an error (Cannot convert null to 'int' because it is a value type).

A nullable type can represent the normal range of values for its underlying value type, plus an additional null value. For example, a Nullable<Int32>, can be assigned any value from -2147483648 to 2147483647, or it can be assigned the null value. A Nullable<bool> can be assigned the values true or false, or null.

To create Nullable Types you must declare your variable this way:

System.Nullable<int> x;
// Then you can assign it to a null value
x = null;

But there is a short way of doing this just add a “?” operator after your type declaration:

int? x = null;

Both two types of the declaration are interchangeable.

By declaring your type as nullable, you have additional properties on your object. These are HasValue and Value read-only properties. Both of these properties is being used to check the nullability of your data. The HasValue property returns true if the variable contains a value, or false if it is null. The Value property returns a value if one is assigned, otherwise a System.InvalidOperationException is thrown. Here’s an example:

int? x = null;
if (x.HasValue) {
   int j = x.Value;
}

Another useful additional function is the GetValueOrDefault. This function returns the current value or the default value of the type if its current value is null.

int? x = null;
int j = x.GetValueOrDefault();

The statement would give the variable j a 0 value instead of a null.

This might be the coolest feature of the nullable types. If your variable is of nullable, then you can use the new “??” operator. This is the shortcut of “?:” operator (iif in VB) on detecting null values.

// This is the old declaration
int? x = null;
int j = x == null ? 0 : x;

// This is the new declaration
int? x = null;
int j = x ?? 0;

Both statements are interchangeable. It’s just in your choice on which looks easier.

This wraps up my first blog. Hope to see you on my next!

Filed under:

Comments

# forced sex said on June 6, 2006 9:01 PM:

Wellcome to the real world.

# opt-ion said on June 7, 2006 1:45 AM:

Cheers.  Just switched to using 2005 and found this helpful.

# Web design miami said on August 23, 2009 9:21 AM:

Good job. I was looking for some detailed reference to the subject.

Thanks for hard work.

Leave a Comment

Name:  
Website: