21

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?

Gordon Gustafson
  • 40,133
  • 25
  • 115
  • 157
  • 4
    shameless plug: I wrote a blog post about how it works a little while ago: http://softwareblog.alcedo.com/post/2010/02/16/Nullable3cT3e-vs-null.aspx – Fredrik Mörk Mar 23 '10 at 21:44
  • @FredrikMörk The link is no longer available... – M. Mimpen Feb 06 '17 at 10:09
  • @M.Mimpen Now it's available again. That blog sort of crashed on me a while ago, but I have republished elsewhere, but have redirects for the original url's. – Fredrik Mörk Feb 25 '17 at 10:11

2 Answers2

37

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
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 1
    How is the boxing handled? Can I implement my own boxing rules for my own structs? –  Mar 23 '10 at 21:45
  • 2
    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 Mar 23 '10 at 21:46
  • 1
    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 Mar 23 '10 at 23:31
  • I just ran across a difference: you can give a `T?` method parameter a default value of `T`, but if you implement your own kind of nullable and try to do the same, you get a compile error. (E.g., "A value of type 'bool' cannot be used as a default parameter as there are no standard conversions to type 'MyNullable'".) Does that fall under one of your bullets? – Rawling Oct 03 '12 at 15:41
4

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
  • 554,122
  • 78
  • 1,158
  • 1,373
  • But it is not simply possible because `T` as well as `Nullable` are structuress and structure cannot be easily set to `null`. – honzakuzel1989 Oct 27 '16 at 08:53