First off, the method name is wrong. You imply that the result of the function is a boolean that is true
exactly if the given value is null. In fact, that’s not the case. GetValueOrDefault
might be a better name.
Secondly, you’re merely replicating the behaviour of the null coalesce operator, as mentioned by others.
Thirdly, your conditional is odd:
Object.Equals(value,null)) | (Object.Equals(value,DBNull.Value)
Why Object.Equals
instead of ==
? Better yet, use Object.ReferenceEquals
since that makes it clear that you’re interested in reference equality. Also, you’re using the bitwise-or operator (|
) which is semantically wrong in this context, although it happens to yield the right value. You want the boolean-or operator ||
. (Also, inconsistency: why do you sometimes write object
and other times Object
?)
Finally, using type object
instead of a generic type isn’t necessarily a good solution. It would be better to create overloads for generic reference and value types: this avoids boxing in the value types. It also means that you don’t have to specify the type explicitly in your second overload since it can be deduced from the method argument.