How to compare nullable types?
if (x.Equals(y))
C# supports "lifted" operators, so if the type (bool?
in this case) is known at compile you should just be able to use:
return x != y;
If you need generics, then EqualityComparer<T>.Default
is your friend:
return !EqualityComparer<T>.Default.Equals(x,y);
Note, however, that both of these approaches use the "null == null
" approach (contrast to ANSI SQL). If you need "null != null
" then you'll have to test that separately:
return x == null || x != y;
You can use the static Equals method on System.Object:
var equal = object.Equals(objA, objB);
Nullable.Equals<T>
?