In C#, without constraints on a generic type parameter, both reference and value types can be passed. However, comparing
this type parameter to null can be misleading as value types, like struct, can never be null.
To avoid unexpected comparisons:
bool IsDefault<T>(T value)
{
if (value == null) // Noncompliant
{
// ...
}
}
bool IsDefault<T>(T value)
{
if (EqualityComparer<T>.Default.Equals(value, default(T)))
{
// ...
}
}
or
bool IsDefault<T>(T value) where T : class
{
if (value == null)
{
// ...
}
}
default operator