In our own Jon Skeet's C# in depth, he discusses the 3 ways to simulate a 'null' for value types:
It is mentioned that nullable types use the third method. How exactly do nullable types work under the hood?
You can declare nullable types using Nullable<t> where T is a type. Nullable<int> i = null; A nullable type can represent the correct range of values for its underlying value type, plus an additional null value. For example, Nullable<int> can be assigned any value from -2147483648 to 2147483647, or a null value.
No, a nullable is a struct.
C# 2.0 introduced nullable types that allow you to assign null to value type variables. You can declare nullable types using Nullable where T is a type. Nullable types can only be used with value types. The Value property will throw an InvalidOperationException if value is null; otherwise it will return the value.
There are two public read-only properties, HasValue and Value , in an instance of a nullable type. While the former is used to check if the nullable variable contains a value, the latter is used to retrieve the value contained inside the nullable variable. Note that HasValue has a default value of false.
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:
EqualityComparer<T>
, Comparer<T>
etc)Nullable<Nullable<T>>
)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With