I have a class with two overrides for the == operator to compare it with other instances of this class and compare with the string instance.
class SomeClass { string value; public SomeClass (string _Value) { value = _Value; } static public bool operator == (SomeClass C1, SomeClass C2) { return C1.value == C2.value; } static public bool operator != (SomeClass C1, SomeClass C2) { return C1.value != C2.value; } static public bool operator == (SomeClass C1, string C2) { return C1.value == (string) C2; } static public bool operator != (SomeClass C1, string C2) { return C1.value != (string) C2; } }
However, when I try to compare this class with null:
Console.WriteLine(someObject == null);
I get the following error:
Error CS0121: The call is ambiguous between the following methods or properties: `SomeClass.operator ==(SomeClass, SomeClass)' and `SomeClass.operator ==(SomeClass, string)'
How do I define overrides == == =========================================== =================================================== ===================================================
equality c # operator-overloading null-check
Max yankov
source share