A simple question:
I want to compare two objects using the virtual Equals() method (not ==
). Both can be null.
Should I repeat this litany:
if ((left == null && right == null) || (left != null && left.Equals(right)) {
}
or is there a more elegant idiom for such situation?
Yup:
if (object.Equals(left, right))
or even without making it obvious that it's calling the static method:
if (Equals(left, right))
(Personally I prefer the extra clarity though.)
The static object.Equals
method doesn't have terribly good documentation, but it does exactly what you want :)
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