New Features in C# 2.0—Nullable Types: How do I do that?

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
CSharp-Online.NET:Tutorials
C# Tutorials

New Features in C# 2.0

© 2005 O'Reilly Media, Inc.

How do I do that?

You can declare a nullable type as follows:

System.Nullable<T> variable

Or, if you are within the scope of a generic type or method, you can write:

T? variable

Thus, you can create two Nullable integer variables with these lines of code:

System.Nullable<int> myNullableInt;
int? myOtherNullableInt;

You can check whether a nullable variable is null in two ways as well. You can check like this:

if (myNullableInt.HasValue)

or like this:

if (myNullableInt != null)

Each will return true if the myNullableInt variable is not null, and false if it is, as illustrated in Example 1-7.

Example 1-7. Nullable types

using System;
namespace NullableTypes
{
  public class Dog
  {
    private int age;
    public Dog(int age)
    {
      this.age = age;
    }
  }
 
  class Program
  {
    static void Main(string[ ] args)
    {
      int? myNullableInt = 25;
      double? myNullableDouble = 3.14159;
      bool? myNullableBool = null; // neither yes nor no
 
      // string? myNullableString = "Hello"; // not permitted
      // Dog? myNullableDog = new Dog(3); // not permitted
      if (myNullableInt.HasValue)
      {
        Console.WriteLine("myNullableInt is " + myNullableInt.Value);
      }
      else
      {
        Console.WriteLine("myNullableInt is undefined!");
      }
      if (myNullableDouble != null)
      {
        Console.WriteLine("myNullableDouble: " + myNullableDouble);
      }
      else
      {
        Console.WriteLine("myNullableDouble is undefined!");
      }
      if ( myNullableBool != null )
      {
        Console.WriteLine("myNullableBool: " + myNullableBool);
      }
      else
      {
        Console.WriteLine("myNullableBool is undefined!");
      }
      myNullableInt = null; // assign null to the integer
 
      // int a = myNullableInt; // won't compile
      int b;
      try
      {
        b = (int)myNullableInt; // will throw an exception if x is null
        Console.WriteLine("b: " + b);
      }
      catch (System.Exception e)
      {
        Console.WriteLine("Exception! " + e.Message);
      }
      int c = myNullableInt ?? -1; // will assign -1 if x is null
      Console.WriteLine("c: {0}", c);
 
      // careful about your assumptions here
      // If either type is null, all comparisons evaluate false!
      if (myNullableInt >= c)
      {
        Console.WriteLine("myNullableInt is greater than or equal to c");
      }
      else
      {
        Console.WriteLine("Is myNullableInt less than c?");
      }
    }
  }
}

Output:

myNullableInt is 25
myNullableDouble: 3.14159
myNullableBool is undefined!
Exception! Nullable object must have a value.
c: -1
Is myNullableInt less than c?


Previous_Page_.gif Next_Page_.gif


Personal tools