I have a class that overloads operator== to compare two objects, however, when I check an object of this type for null , then I get an exception using a NULL reference for the first parameter. I wondered how I should defend such a case, or is there another way to implement this operator==
Card c; if (c == null) { // do something } //null check throws exception cause c1 in operator has is a null object... public static bool operator ==(Card c1, Card c2) { if (ReferenceEquals(c1, null) ) return false; // this does not make sense either I guess?? return c1.Equals(c2); }
source share