May 25

As I’ve said, a variable holding a value type value can never be null. The justification for this behavior is obvious: the variable contains the value itself (unlike reference types, where the variable holds a reference to another memory location in the heap). And life was fine until someone noticed that it would be really useful to say that value type variable holds value null. “What?”, you say, “that makes no sense!”. And you’re probably right. In theory, there’s no place for null in value types…at least, not until you need to load a value from a database’s table column which is allowed to have nulls…and that’s why MS introduced the Nullable<T> struct. Lets start with some code:

Nullable<Int32> aux = 10;
Nullable<Int32> aux2 = null;
Console.WriteLine("aux, has value: {0}, value: {1}", aux.HasValue, aux.Value);
Console.WriteLine("aux2, has value: {0}, value: {1}", aux2.HasValue, aux2.GetValueOrDefault());

Running the previous code returns the following results in my machine:



There’s already a lot going on in the previous example:

  • As you can see, we can initialize a Nullable<T> variable with T or with null (you could also declare a variable without initializing it for getting the same result. I’ve went with the null assignment to simulate getting a value from a data reader).
  • The HasValue property returns true when the Nullable<T> private field holds a “non-null” value (ie, when the instance wraps a T value).
  • The Value property returns the “current” value type value wrapped by the Nullable<T> instance (notice that the property throws if HasValue is false).
  • There’s also a GetValueOrDefault method which returns the current value (when there’s one) or T’s default value when the HasValue property returns false. There’s also an overload of this method which receives a value which is return when the struct doesn’t hold a “valid” value type value.

Nullable<T> is just a lightweight generic struct which wraps a value type field. Besides that field, it will also store a Boolean field which is used for checking if the current Nullable<T> instance holds a valid value. The struct exposes a constructor which receives a T value used for initializing the internal fields. The struct introduces a couple of operators too:

  • There’s an *implicit* operator which can transform all T values into Nullable<T> instances (that’s why we can instantiate Nullable<T> variables with the code shown in the previous snippet).
  • There’s an *explicit* operator which is able to cast from Nullable<T> to T. Internally, the operator relies on Nullable<T>’s Value property.

Finally, the Nullable<T> struct overrides the Equals, GetHashCode and ToString methods so that you can compare Nullable<T>s and get a string which better represents its state. If you’re a C# developer, then you’ll be glad to know that there’s a simplified syntax for using nullable value types from your code. And that’s what we’ll see in the next post. Stay tuned for more.

4 comments so far

  1. Jordan Terrell
    6:50 pm - 5-27-2011

    Nice blog post. You might want to check out the Maybe monad for similar capability. I’ve got an implementation of it, Maybe, which you can learn more about here:

  2. Jordan Terrell
    6:54 pm - 5-27-2011

    WordPress stripped out part of my comment – the implementation is Maybe (sans the spaces that I think WordPress needs so it doesn’t filter the text).

  3. Jordan Terrell
    6:56 pm - 5-27-2011

    Grrr, WordPress is stripping out greater than and less than characters. Anyways, check it out 🙂

  4. luisabreu
    8:59 pm - 5-27-2011

    Very interesting Jordan. thanks for the link!