Since I was wondering about the efficiency of certain operations on Nullable<T>
, I went to check the reference source on the Microsoft site (and learned that HasValue
and GetValueOrDefault
don't involve any switching whereas .Value
does). Looking through the file, I noticed that it is an immutable type with an implicit conversion from T
,
public static implicit operator Nullable<T>(T value) {
return new Nullable<T>(value);
}
which I suppose allows me to write a line like
int? x;
x = 3;
However, this does not explain how
x = null;
works. Nullable
is a value type, so in general, this is not allowed. Initially I expected an overload of operator=
or something similar, but I could not figure out what the argument of that should be (apart from a Nullable<T>
).
I guess then that the compiler knows about Nullable
s and treats them in a special way, but I would guess that the development team would have wanted to keep this to a minimum so I am wondering what exactly the compiler knows. How does it handle a line like x = null
and what other things that I could not find in the reference implementation does it do behind the scenes?