我如何重写相等操作符==在C#中的接口? [英] How do I override the equals operator == for an Interface in C#?
问题描述
我已经定义如下界面
公共接口IHaveAProblem
{
串问题{搞定;组; }
}
和这里是IHaveAProblem实施
公共类SomeProblem:IHaveAProblem
{
公共字符串问题{搞定;组; }
公众覆盖布尔等于(obj对象)
{
SomeProblem otherObj = OBJ为SomeProblem;
如果(otherObj == NULL)
{
返回FALSE;
}
返回this.Issue == otherObj.Issue;
}
公共覆盖INT的GetHashCode()
{
返回base.GetHashCode();
}
公共静态布尔运算符==(RHS SomeProblem,SomeProblem左)
{
//空检查
如果(Object.ReferenceEquals(RHS ,NULL)|| Object.ReferenceEquals(左,NULL))
{
如果(Object.ReferenceEquals(右,空)及和放大器; Object.ReferenceEquals(左,NULL))
{
//都为null。他们这样做等于彼此
返回真;
}
//只有1为null,另一种是不使他们不等于
返回FALSE;
}
返回rhs.Equals(左图);
}
公共静态布尔运算符!=(RHS SomeProblem,SomeProblem左)
{
//空检查
如果(Object.ReferenceEquals(RHS ,NULL)|| Object.ReferenceEquals(左,NULL))
{
如果(Object.ReferenceEquals(右,空)及和放大器; Object.ReferenceEquals(左,NULL))
{
//都为null。他们这样做等于彼此
返回false;
}
//只有1为null,另一种是不使他们不等于
返回真;
}
返回rhs.Equals(左)!;
}
}
当我使用的对象,我可以得到正确的结果对于==比较。
SomeProblem firstTest =新SomeProblem()
{
=问题你好世界
};
SomeProblem secondTest =新SomeProblem()
{
发行=Hello World的
};
//这是真实的
布尔结果= firstTest == secondTest;
然而,当我尝试它做内存接口比较比较,而不是操作上的== SomeProblem
IHaveAProblem firstProblem =新SomeProblem()
{
问题=Hello World的
};
IHaveAProblem secondProblem =新SomeProblem()
{
发行=Hello World的
};
是否有可能有界面使用==上SomeProblem而不是内存比较?
我知道我可以做一个 firstProblem.Equals(secondProblem)
,并得到正确的结果,但我创造框架的工作,我不知道它是如何在末尾。我以为==会正常工作。
运营商 ==
是静态的。您不能定义在C#接口的静态方法。此外,参数类型的所有运营商至少有一个必须作为类它被定义在,因此同类型:无操作符重载的接口:(
你可以做的是使用抽象类,而不是 - 并定义运营商那里再次,运营商可能不是虚拟的(因为静态方法不能是虚拟的...)
<。 p>
I have defined the following interface
public interface IHaveAProblem
{
string Issue { get; set; }
}
And here is the implementation of the IHaveAProblem
public class SomeProblem : IHaveAProblem
{
public string Issue { get; set; }
public override bool Equals(object obj)
{
SomeProblem otherObj = obj as SomeProblem;
if (otherObj == null)
{
return false;
}
return this.Issue == otherObj.Issue;
}
public override int GetHashCode()
{
return base.GetHashCode();
}
public static bool operator ==(SomeProblem rhs, SomeProblem lhs)
{
// Null check
if (Object.ReferenceEquals(rhs, null) || Object.ReferenceEquals(lhs, null))
{
if (Object.ReferenceEquals(rhs, null) && Object.ReferenceEquals(lhs, null))
{
// Both are null. They do equal each other
return true;
}
// Only 1 is null the other is not so they do not equal
return false;
}
return rhs.Equals(lhs);
}
public static bool operator !=(SomeProblem rhs, SomeProblem lhs)
{
// Null check
if (Object.ReferenceEquals(rhs, null) || Object.ReferenceEquals(lhs, null))
{
if (Object.ReferenceEquals(rhs, null) && Object.ReferenceEquals(lhs, null))
{
// Both are null. They do equal each other
return false;
}
// Only 1 is null the other is not so they do not equal
return true;
}
return !rhs.Equals(lhs);
}
}
When I use the object I can get the correct results for the == compare.
SomeProblem firstTest = new SomeProblem()
{
Issue = "Hello World"
};
SomeProblem secondTest = new SomeProblem()
{
Issue = "Hello World"
};
// This is true
bool result = firstTest == secondTest;
However when I try to compare the interfaces it is doing a memory compare rather than the operator == on SomeProblem
IHaveAProblem firstProblem = new SomeProblem()
{
Issue = "Hello World"
};
IHaveAProblem secondProblem = new SomeProblem()
{
Issue = "Hello World"
};
Is it possible have the interface use the == on SomeProblem rather than a memory compare?
I know I can do a firstProblem.Equals(secondProblem)
and get the proper result, however I am creating a frame work and I will not know how it is used in the end. I thought == would work correctly.
The operator ==
is static. You cannot define static methods for interfaces in C#. Also, for all operators at least one of the argument types needs to be of the same type as the class it is defined in, therefore: No operator overloading for interfaces :(
What you CAN do is use an abstract class instead - and define the operator there. Again, the operator may NOT be virtual (since static methods cannot be virtual...)
[Edited, reason see comment.]
这篇关于我如何重写相等操作符==在C#中的接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!