Nullable types

In programming, nullable types are a feature of some statically-typed programming languages which allows a data type to be set to the special value NULL instead of their common range of possible values. For value types or built-in data types like integers and booleans however, such behavior is mostly not possible. Nullable type support allows for the programmer to make also these value types NULL. This can be useful in general and also when working with databases. A field in a Relational database like SQL may have an entry that is NULL (or empty) instead of containing a value. A language with nullable type support can then return a NULL value and correctly represent the behavior of the database. In programming languages like C# 2.0 a Nullable integer for example can be declared by a question mark (int? x)

Database columns can have value as NULL or the column value is blank. This is a common problem when writing code to access database column data. The data might be null, blank or filled with some data. So languages like C#, support the null type. Nullable types are value types which are wrappers round other value types , allowing a "null value" to be represented. They are particularly useful with database code, as databases often have nullable columns for numbers, dates and times, GUIDs etc – all of which are value types in .NET. The Nullable<T> structure is at the heart of nullable types, and C# has extra syntactic sugar with the "?" modifier. For example:

   1: using System;

   2: using System.Collections.Generic;

   3: using System.Linq;

   4: using System.Text;


   6: namespace ConsoleApplication1

   7: {

   8:     class Program

   9:     {

  10:         static void Main(string[] args)

  11:         {

  12:             // x, y and z are the same type

  13:             int? x = null;

  14:             Nullable<int> y = 10;


  16:             int? z = x + y;


  18:             if (y == null)

  19:             {

  20:                 Console.WriteLine("Null result");

  21:             }

  22:             else

  23:             {

  24:                 // Note: not nullable

  25:                 int result = y.Value;

  26:                 Console.WriteLine("Result: {0}", result);

  27:             } 


  29:         }

  30:     }

  31: }

  • The CLR knows about nullable types too – it makes sure that if you box the null value of a nullable type, you end up with a null reference. (And likewise you can unbox a null reference to the null value of a nullable type.)
  • How operators work with nullable types is a language decision. For instance, comparing two int? values for equality always gives true or false in C#; in VB.NET it can result in null (if either side is null). Be careful when porting code!

The null coalescing operator can be used with reference types, and the right hand side can also be a reference type or a nullable type. It can be useful to use the operator several times in a row, e.g. first ?? second ?? third. Indeed, the language is designed to enable this to work exactly how you’d like it to. A value type constraint explicitly excludes any nullable types.

Nullable types have the following characteristics (very useful MSDN article):












    About these ads

    Leave a Reply


    You are commenting using your account. Log Out / Change )

    Twitter picture


    Facebook photo


    Google+ photo


    Connecting to %s