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?
Adding to the answers posted here, I think we should also be able to specify if you we want value or reference equality:
We all just love to create and maintain a zillion little helper methods like that don't we :->
Throw in the class constraint and it should work.
Or if you only want value types you can do this.
Your problem is that a genric type (with no constraint) has to be "compilable" for any type. Since not all types have an == operator your code will not compile.
One way to resolve it is to add a class constraint however since your using default(TValue) that would suggest you want the code to work with other types. (otherwise just use null instead of default(TValue). One solution might be something similar to what Bryan Watts suggests
or you could wrap it up in an extension method