0

I am implementing IComparible with

public int CompareTo(RangeEx range)
{
    if (range == null)
                return 1;
// more stuff
}

public static bool operator ==(RangeEx x, RangeEx y) { return x.CompareTo(y) == 0; }

The problem is the "if (range == null)" calls the operator overload. How can I do the check for null by not calling the overload?

David Thielen
  • 28,723
  • 34
  • 119
  • 193
  • You cannot do that, instead you must fix your operator code so that it can safely handle `null` references. Or you can change your if-statement, try `if (range is null)` instead, assuming you're on C# 7, or you can do `if (ReferenceEquals(range, null))`. – Lasse V. Karlsen Oct 11 '18 at 19:39
  • `if (ReferenceEquals(range, null))` is the standard correct way of doing this. – itsme86 Oct 11 '18 at 19:40

0 Answers0