Consider the following code:
int? x = null;
Console.Write ("Hashcode: ");
Console.WriteLine(x.GetHashCode());
Console.Write("Type: ");
Console.WriteLine(x.GetType());
When executed, it writes that Hashcode is 0
, but fails with NullReferenceException
in attempt to determine type of x
.
I know that methods called on nullable types are actually called on underlying values, so I expected program to fail during x.GetHashCode()
.
So, what is the fundamental difference between those two methods and why doesn't the first of them fail?