Why does Foo<int?> throw an exception?
static void Foo<T>() where T : new()
{
T t = new T();
Console.WriteLine("ToString: " + t.ToString());
Console.WriteLine("GetHashCode: " + t.GetHashCode());
Console.WriteLine("Equals: " + t.Equals(t));
Console.WriteLine("GetType: " + t.GetType());
}
public void Main()
{
Foo<int>();
Foo<int?>(); // Exception thrown
}
Answer
int? is a nullable type. T t = new T(); returns a Nullable
[Serializable]
public struct Nullable<T> where T : struct
{
public Nullable(T value);
public static explicit operator T(Nullable<T> value);
public static implicit operator Nullable<T>(T value);
public bool HasValue { get; }
public T Value { get; }
public override bool Equals(object other);
public override int GetHashCode();
public T GetValueOrDefault();
public T GetValueOrDefault(T defaultValue);
public override string ToString();
}
As you can see, Nullable
But GetType is another story. GetType only exists on the base class Object and cannot be overridden. When calling GetType on a Nullable
int i = 123;
// The following line boxes i.
object o = i;
If you have a Nullable