Nullable Types in C#

In .NET 1.0 and 1.1, if you wanted to store a null value for a value type (int, double, DateTime, etc) you would have to box it or write your own wrapper around it. The introduction of generics in .NET 2.0 allowed the concept of nullable types to be introduced into the language through the Nullable<t> structure.

Now if you want an int value that could be null, you could write,

<span style="color: #800080">Nullable</span><span style="color: #ff0000">&lt;</span><span style="color: #0000ff">int</span><span style="color: #ff0000">&gt;</span> x <span style="color: #ff0000">=</span> <span style="color: #0000ff">new</span> <span style="color: #800080">Nullable</span><span style="color: #ff0000">&lt;</span><span style="color: #0000ff">int</span><span style="color: #ff0000">&gt;</span>( <span style="color: #800080">12</span> );

This is a fair amount of typing, so luckily, there is compiler support for nullable types using the ? operator.

int? y = null;
int? z = 14;

You can test if a value is null with the HasValue property, or you can simply compare to null.

<span style="color: #0000ff">if</span> ( x<span style="color: #ff0000">.</span>HasValue )
{
   <span style="color: #008000">// ...
</span>}
<span style="color: #0000ff">if</span> ( y <span style="color: #ff0000">==</span> <span style="color: #0000ff">null</span> )
{
   <span style="color: #008000">// ...
</span>}

You can add two nullable types together and they will preserve the null values.

<span style="color: #0000ff">int</span><span style="color: #ff0000">?</span> i <span style="color: #ff0000">=</span> x <span style="color: #ff0000">+</span> y;   <span style="color: #008000">// i == null
</span><span style="color: #0000ff">int</span><span style="color: #ff0000">?</span> j <span style="color: #ff0000">=</span> x <span style="color: #ff0000">+</span> z;   <span style="color: #008000">// j == 26 </span>

To assign a nullable type back to a value type, you use the Value property.

<span style="color: #0000ff">int</span> k <span style="color: #ff0000">=</span> j<span style="color: #ff0000">.</span>Value;

The only problem with the above is that it will throw an exception if the value is null. To get around this, always check for null first, or use the GetValueOrDefault method.

<span style="color: #0000ff">int</span> l <span style="color: #ff0000">=</span> i<span style="color: #ff0000">.</span>GetValueOrDefault();      <span style="color: #008000">// l == 0
</span><span style="color: #0000ff">int</span> m <span style="color: #ff0000">=</span> i<span style="color: #ff0000">.</span>GetValueOrDefault( <span style="color: #ff0000">-</span><span style="color: #800080">1</span> );  <span style="color: #008000">// m == -1;</span>
<span style="color: #008000"></span>

Nullable types are fairly simple, but really powerful when you need them. Make sure they are in your tool belt.

4 thoughts on “Nullable Types in C#

  1. Actually, ?? works on any reference or Nullable type.

    For example,

    string IamNull = null;
    string nonNull = “foo”;
    string s = IamNull ?? nonNull;

    s gets set to nonNull

  2. Don’t forget the ?? shortcut for GetValueOrDefault

    int m = i ?? 0;

    m will have the value 0 if i is null, or i if it is not null.

  3. You don’t have to use the .HasValue property. The compiler is smart enough to figure out what you mean if you say something like

    int? i = 5;
    if(i != null)
    {
    // use it here
    }

Comments are closed.