tags:

views:

158

answers:

2

In our own Jon Skeet's C# in depth, he discusses the 3 ways to simulate a 'null' for value types:

  • Magic value (e.g. earliest possible DateTime is taken to be 'null')
  • Reference type wrapper
  • boolean flag

It is mentioned that nullable types use the third method. How exactly do nullable types work under the hood?

+14  A: 

Ultimately, they are just a generic struct with a bool flag - except with special boxing rules. Because structs are (by default) initialized to zero, the bool defaults to false (no value):

public struct Nullable<T> where T : struct {
    private readonly T value;
    private readonly bool hasValue;
    public Nullable(T value) {
        this.value = value;
        hasValue = true;
    }
    public T Value {
        get {
           if(!hasValue) throw some exception ;-p
           return value;
        }
    }
    public T GetValueOrDefault() { return value; }
    public bool HasValue {get {return hasValue;}}
    public static explicit operator T(Nullable<T> value) {
        return value.Value; }
    public static implicit operator Nullable<T>(T value) {
        return new Nullable<T>(value); }
}

Extra differences, though:

  • special boxing rules (you can't normally do this)
  • special C# rules for comparing to null etc
  • "lifted" operators in C# (and in .NET via EqualityComparer<T>, Comparer<T> etc)
  • special rules on generic type constraints (to prevent Nullable<Nullable<T>>)
Marc Gravell
How is the boxing handled? Can I implement my own boxing rules for my own structs?
jdv
It is handled by special rules in the CLI. No, you *cannot* do this yourself. Likewise the "lifted" operators aren't something you can do yourself.
Marc Gravell
It's also worth noting that the behaviour of operators is governed by the language - C# and VB have different rules on this for various operators.
Jon Skeet
+2  A: 

Nullable<T> works by providing two fields:

private bool hasValue;
internal T value;

The properties work from those. If you set it to null, hasValue is set to false.

Reed Copsey