The following doesn\'t compile:
public void MyMethod(T value)
{
if (value == default(T))
{
// do stuff
}
}
To avoid boxing for struct
/ Nullable
, I would use:
if (EqualityComparer.Default.Equals(value,default(T)))
{
// do stuff
}
This supports any T
that implement IEquatable
, using object.Equals
as a backup, and handles null
etc (and lifted operators for Nullable
) automatically.
There is also Comparer
which handles comparison tests. This handles T
that implement IComparable
, falling back to IComparable
- again handling null
and lifted operators.