Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
edited tags
Link
Alexei Levenkov
  • 101.1k
  • 15
  • 138
  • 189
Source Link
Andrew Jones
  • 5.4k
  • 8
  • 28
  • 19

How do I check for nulls in an '==' operator overload without infinite recursion?

The following will cause infinite recursion on the == operator overload method

 Foo foo1 = null; Foo foo2 = new Foo(); Assert.IsFalse(foo1 == foo2); public static bool operator ==(Foo foo1, Foo foo2) { if (foo1 == null) return foo2 == null; return foo1.Equals(foo2); } 

How do I check for nulls?