Is there a way to compare nullable and non-nullable generics in C#?
For example:
public void function<T>()
{
Type t = sqlreader.GetValue(pos).GetType();
}
where t is of type Int32 and T is of type Nullable<Int32>.
How can we compare t and T such that it returns true?
Call
Nullable.GetUnderlyingType(t).If
tis aNullable<X>, this will returntypeof(X); otherwise, it will returnnull.Therefore, you can write