RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Working with Nullable Types in C#

Use nullable types to assign null values to value types and avoid run-time exceptions in your applications.

orking with value types was sometimes awkward in .NET 1.x, because they could cause serious problems in applications that retrieved data from database tables containing columns that can have null values—those that may or may not hold data. C# 2.0 presents a new concept called "nullable types" that lets you initialize value types to null values. This article discusses how you can apply the new nullable types feature to solve issues that result from having null values in database columns.

Why Nullable Types?
Value types are "primitive" data types such as numbers. The .NET Framework stores value types in the stack and implicitly initializes them to their default values—even if you do not explicitly initialize them when they are defined. For example, an int gets initialized to 0, a bool to false, and so on. Unfortunately, most value types are unable to represent a null value, which presents a problem when you're working with data-centric applications where null values are possible, because you have to select some other value to represent null. But whatever value you choose to represent null is no longer available as a valid data value, which restricts the range of permissible values. In other words, if you elect to use -1 to represent null, you've removed -1 as a valid value from the range of numbers that the value type can support. In addition, you must check to ensure that the chosen "null-representation" values are ignored in other parts of the application—and you must make sure not to display the null-representation values directly to end users, who may not understand that the value represents null.

Developers have tried various approaches to solve the null-value type mismatch, but each has its associated pitfalls. This is where nullable types can help. They're perfect for situations where you may need to represent a value type in an undefined state.

The most common such situation occurs when you want to assign the value from a column of a database table (that allows for null values) to a C# type.

The System.Nullable Structure
C# 2.0 provides a System.Nullable generic type struct that you can use to define nullable types. The constructor accepts one parameter—the type itself—and is defined as shown below:

   namespace System
      public struct Nullable : System.IComparable, 
         public Nullable(T value);
         public static explicit operator T(T? value);
         public static implicit operator T?(T value);
         public T Value { get; }
         public bool HasValue { get; }
         public T GetValueOrDefault();
Here's the syntax you use to define a nullable type:

   System.Nullable variable = null;
Note that the generic type T in the preceding code stands for a value type, not a reference type.

As a more concrete example, you can define a nullable integer type as follows:

   System.Nullable patientBilledAmount;
The long version above has an equivalent shorthand version:

   int? patientBilledAmount;
In C# 2.0, a question mark (?) suffix following a data type designator specifies that the type can accept null values.

If you refer back to the nullable struct definition shown earlier, notice that any nullable type contains two properties that you use in tandem to check for null values. If the type contains a non-null value, the HasValue property returns true; otherwise it returns false. On the other hand, the Value property returns a non-null value if and only if the HasValue property returns true; otherwise, it throws an exception. So typically, you check nullable type values as follows:

   if (patientBilledAmount.HasValue)
      Console.WriteLine("The amount is null");
Author's Note: Always perform the HasValue check, because if you try to retrieve the value of a nullable type using its Value property, and HasValue is false, the .NET Framework throws an InvalidOperationException.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date