19 January 2014

nullable types in C#

Nullable types in C# .NET allows assigning null values to structures. By declaring a structure as nullable type, null value can be assigned to structure variable.

In this tutorial, let's understand more about nullable types, by going through the below sections:

  1. Why nullable types?
  2. How to declare nullable types?
  3. Working with APIs of nullable types with examples
  4. How .NET has defined nullable types?

Why nullable types?

Let's consider an example of developing a Customer management application; The application stores the Customers details including Date of Birth and Annual Salary. As per business requirement, a record can exist with no values for the Date of Birth and Annual Salary fields. So when it comes to DB design, the columns to store these two fields will be nullable.

While designing app layer contracts, you will start thinking about business entities in the system. The Customer entity class has Date of Birth and Annual Salary properties. So you declare Date of Birth as DateTime & Annual Salary as Integer type. Both DateTime and Integer are Structures and they cannot have null values. But application needs these two fields to have null when customer hasn't specified values. But null value is not supported for structure data types.

public class Customer 
{
 public DateTime DateOfBirth {get;set;}
 public int AnnualSalary {get;set;}
}

So there is a problem. This problem can be workaround by assigning zero to Salary and the corresponding interpretation in UI layer to display Not Specified when value is zero.

The workaround for DateTime field is not easy. The default value for DateTime structure in .NET is less than allowed value in MS SQL server database DateTime column. So you need to do more workaround which isn't straight forward and your fellow developers will have difficult time in understanding such code and hence code will become less maintainable. So these work around are not clean design.

These problems can be solved by making .NET structure fields to have null value. This can be achieved using nullable structure types. Then, let's see how?

How to declare nullable types?

Nullable types declared with "question mark - ?" suffixed to the structure data type like below:

public DateTime? DateOfBirth {get;set;}
public int? AnnualSalary {get;set;}

Working with APIs of nullable types with examples

Nullable types notably has two properties HasValue and Value and GetValueOrDefault method with two overloads.

  • HasValue: HasValue boolean property indicates whether nullable type has any value other than null.
    DateTime? DateOfBirth {get;set;}
    if(DateOfBirth.HasValue) {
     // Nullable type has valid value.
    }
  • Value: Value is a property of underlining structure type. Value is read-only property, you cannot assign any value directly to this.
    DateTime? DateOfBirth {get;set;}

    if(DateOfBirth.HasValue) 
    {
      DateTime value = DateOfBirth.Value;
    }
  • GetValueOrDefault: This method on the Nullable type returns the underlining structure value if has value else returns the default value of the underlining structure type.
    int? annualSalary;

    // This returns zero; because 
    // integer value type default value is zero
    int salary = annualSalary.GetValueOrDefault();

    // Overloaded method taking default value 
    // to return if nullable type value is null.
    salary = annualSalary.GetValueOrDefault(1213)

How .NET has defined nullable types

In mscorlib.dll assembly Nullable types are declared as structure with Generics where T can only be a value type.

public struct Nullable where T : struct
 {

 }

Reference types like classes cannot be declared as nullable. If you do so, you will get the below error:

The type must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable' GetValueOrDefault

No comments:

Post a Comment