我有一个类,有两个覆盖 == 运算符,将其与此类的其他实例进行比较,并与字符串的实例进行比较。
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;
}
}
但是,当我尝试将此类与 null 进行比较时:
Console.WriteLine(someObject == null);
我收到以下错误:
Error CS0121: The call is ambiguous between the following methods or properties: `SomeClass.operator ==(SomeClass, SomeClass)' and `SomeClass.operator ==(SomeClass, string)'
我应该如何定义我的 == 覆盖,以便我仍然可以对此类的实例进行空检查?