I want to be able to check whether a value is the default for its value type. Ideally, I'd like to say:
DoSomething<TValue>(TValue value) {
if (value == default(TValue)) {
...
}
}
However, the compiler complains that it can't do a ==
comparison on TValue and TValue. This is the best workaround that I've come up with so far:
DoSomething<TValue>(TValue value) {
if (value == null || value.Equals(default(TValue))) {
...
}
}
Is there a more elegant/correct way to go about this?
public bool EqualsDefaultValue<T>(T value)
{
return EqualityComparer<T>.Default.Equals(value, default(T));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With