how are nullable types implemented under the hood in .net?

后端 未结 2 1990
自闭症患者
自闭症患者 2020-11-29 11:56

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 b
相关标签:
2条回答
  • 2020-11-29 12:38

    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>>)
    0 讨论(0)
  • 2020-11-29 12:43

    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.

    0 讨论(0)
提交回复
热议问题