I am trying to write operator overload for custom class and don’t know how to handle null comparison.
Class Customer { int id; public static bool operator ==(Customer a, Customer b) { //When both a and b are null don't know how to compare as can't use == in here as //it will fall into loop } }
Problem is when a and b both are null result is true but not sure how to check it without using ==.
ReferenceEquals() checks if they are pointing to the exact same object (or if they are both null)
(As a general rule, it’s good to start an Equals() method with a call to ReferenceEquals, particularly if the rest of the method is complicated. It will make things like
a==aquickly return true, instead of having to check every element.)