I was wondering if anyone knows how the C# compiler handles the following assignment:
int? myInt = null;
My assumption is that there is an
What really happens is that when you assign null to the nullable type instance, the compiler simply creates a new instance of T?, using the default constructor (the initobj IL instruction on Jb answer), so the following two lines are equivalent:
int? a = null;
Nullable b = new Nullable();
object.Equals(a,b); // true
Therefore you cannot do this:
Nullable c = new Nullable(null);
Something similar happens then you compare a nullable type to null:
if (a == null)
{
// ...
}
Behind the scenes it just does a call to the a.HasValue property.