I have a few places where I need to compare 2 (nullable) values, to see if they\'re the same.
I think there should be something in the framework to support this, but can
Nullable.Equals<T>?
if (x.Equals(y))
(x?? 0).Equals(y)
will handle null as well as equals.
Just use ==
, or .Equals()
.
I wanted to find how to compare two nullable int on C#, but I always get this link after search, so if someone needs to compare exactly two nullable int, then this can be helpful
a.GetValueOrDefault(int.MinValue).CompareTo(b.GetValueOrDefault(long.MinValue));
You can use the static Equals method on System.Object:
var equal = object.Equals(objA, objB);