Gil Fink on .Net

Fink about IT

News

Microsoft MVP

MCPD Enterprise Applications Developer

Gil Fink

My Linkedin profile

Locations of visitors to this page

Creative Commons License

Blog Roll

Hebrew MSDN Articles

Index Pages

My OSS Projects

Back to Basics – Nullable Value Type

Back to Basics – Nullable Value Type

In the following posts Nullable Value Type
I’m going to describe
basic tools which every
developer needs to
know. In today’s post
I’m going to explain
what is a Nullable value type and how
to use it in your code.

What are Nullable Value Types?

Nullable value types are an instances of the Nullable<T> struct.
The nullable value type can hold the all the values that its underlining value type holds
and a null value. The main reason for this type came from the use of databases.
In databases, data can be null (or if you prefer DBNull) and when the developers
retrieved the null value instead of a value type they expected a problem was raised. 
What can you do with the database null value? should you use the default value for
the value type which can cause logical or business errors? should you use minimum
value to indicate null? Using the nullable value type helped to remove those questions. 
The use of nullable value type can grantee a simple and clean way to hold a value type
even if it was returned from the database as null.

How to Use Nullable Value Types?

There are two ways to create a nullable value type:

  • By using the ? type modifier -

       int? someInt;

  • By using the Nullable<T> struct -

       Nullable<int> someInt;

The nullable value type has two properties that can be used by the developer:

  • HasValue – returns true if the nullable value type holds a value.
    Returns false if the nullable value type holds null.
  • Value – returns the value that the nullable value types holds.
    The property should always be accessed after you check if the nullable value
    type
    has a value otherwise an InvalidOperationException exception will be thrown.

Another thing to know about a nullable value type is the GetValueOrDefault method.
That method returns the value inside of the nullable value type or the default value for the
underlining value type if the nullable value type is null.
An example of how to read a value from a nullable value type can look like:

   int? someNumber = null;

   if (someNumber.HasValue)

   {

      Console.WriteLine("the value is {0}",

         someNumber.Value);

   }

   else

   {

      Console.WriteLine("There is no value");

   }

The code above will produce the “There is no value” sentence in the console.
A restriction that needs to be followed is that you can’t have nested nullable value types.
The following code won’t even compile:

   Nullable<Nullable<int>> someNumber;

Summary

The post introduced the concept of nullable value types.
You should use the type on members that are being returned from a database columns
which can hold null values. The use of the nullable value types is simple and straight forward.

Comments

Adam Zochowski said:

Too bad nullable types do not behave like SQL nulls.

if ( null=null )

  print 'true'

else

  print 'false'

This prints false. The proper way.  Microsoft, in their never ending wisdom decided to break this.

int? a = null;

int? b = null;

if (a==b)

  System.Console.WriteLine('true')

else

  System.Console.WriteLine('false');

Why? So far no answer. I had hopes that finally .NET could be language of future, taking best from SQL, such as forcing handling string collations, and nullable types. Alas, still not perfect. ;D

# October 3, 2008 5:56 PM

some guy said:

Fascinating

# October 3, 2008 8:32 PM

Gil Fink said:

Thanks for the replies Adam and some guy.

For the things Adam said it's true. Nullable types don't behave exactly like SQL nulls but they surely close to their behavior.

# October 4, 2008 2:21 AM