I was wondering if anyone knows how the C# compiler handles the following assignment:
int? myInt = null;
My assumption is that there is an
The statement:
int? myInt = null;
Gets compiled as:
.locals init (valuetype [mscorlib]System.Nullable`1 V_0)
IL_0000: ldloca.s V_0
IL_0002: initobj valuetype [mscorlib]System.Nullable`1
Which, according to the MSDN, means «Initialize each field of the value type at a specified address to a null reference or a 0 of the appropriate primitive type.»
So there's no constructor or conversion here. HasValue will return false, and trying to get its Value will throw an InvalidOperationException. Unless you use GetValueOrDefault of course.