C# shorthand for Equals() when both args can be null

c#, equals, null

Solution

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 :)

Problem

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?

Original source