why is typeof int?
an Int32
int? x = 1;
Console.WriteLine(x.GetType().Name);
If it is okay then what\'s the use of
Mainly its for dealing with a generic method:: e.g.
public static void SomeMethod(T argument)
{
if(Nullable.GetUnderlyingType(typeof(T) != null)
{
/* special case for nullable code go here */
}
else
{
/* Do something else T isn't nullable */
}
}
It's important to know this, as certain things that are very cheap can be wildly expensive on nullable's. For instance, if(argument == null)
is normally super cheap, but when done in a generic method on a Nullable
is forced to box the argument
to get a null reference. Your best bet is to use EqualityComparer
which will slow everything else down, but makes nullable's not suffer.