当使用接口而不是实际类型时,如何覆盖运算符 == ?
我有一些这样的代码:
我应该如何实现运算符 == 以便当变量属于接口 IMyClass 时调用它?
public class MyClass : IMyClass
{
public static bool operator ==(MyClass a, MyClass b)
{
if (ReferenceEquals(a, b))
return true;
if ((Object)a == null || (Object)b == null)
return false;
return false;
}
public static bool operator !=(MyClass a, MyClass b)
{
return !(a == b);
}
}
class Program
{
static void Main(string[] args)
{
IMyClass m1 = new MyClass();
IMyClass m2 = new MyClass();
MyClass m3 = new MyClass();
MyClass m4 = new MyClass();
Console.WriteLine(m1 == m2); // does not go into custom == function. why not?
Console.WriteLine(m3 == m4); // DOES go into custom == function
}
}
I have some code like this:
How should I implement the operator == so that it will be called when the variables are of interface IMyClass?
public class MyClass : IMyClass
{
public static bool operator ==(MyClass a, MyClass b)
{
if (ReferenceEquals(a, b))
return true;
if ((Object)a == null || (Object)b == null)
return false;
return false;
}
public static bool operator !=(MyClass a, MyClass b)
{
return !(a == b);
}
}
class Program
{
static void Main(string[] args)
{
IMyClass m1 = new MyClass();
IMyClass m2 = new MyClass();
MyClass m3 = new MyClass();
MyClass m4 = new MyClass();
Console.WriteLine(m1 == m2); // does not go into custom == function. why not?
Console.WriteLine(m3 == m4); // DOES go into custom == function
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
关键是您没有重写某个运算符,而是重载它。
没有定义任何运算符,
因此编译器无法调用任何内容。它无法调用
,因为它不知道
m1
和m2
实际上会引用MyClass
的实例。据我所知,没有办法实现用于接口的运算符 - 毕竟,多个实现都可以提供自己的一个,只是为了可能存在歧义。
就我个人而言,我对一开始就尝试谈论非密封类型的平等有点谨慎——平等和继承不能很好地混合。当然,这对于接口来说是双重的:)您最好实现一个适当的 IEqualityComparer并使用它。
The key is that you're not overriding an operator - you're overloading it.
There's no operator defined for
so the compiler has nothing it could call. It can't call
as it doesn't know that
m1
andm2
will actually refer to instance ofMyClass
.As far as I know there's no way of implementing an operator to be used for interfaces - after all, multiple implementations could all provide their own one, just for one point of possible ambiguity.
Personally I'm somewhat wary of trying to talk about equality over non-sealed types to start with - equality and inheritance don't mix terribly nicely. That goes doubly for interfaces, of course :) You might be best implementing an appropriate
IEqualityComparer<IMyClass>
and using that instead.查看此问题的已接受答案: == vs. Object.Equals(object) in .NET
区别在于身份平等与语义平等之一。重写 == 意味着应该是“相同”的结构,因为它们具有相同的值,因为结构是按值复制的,因此永远不会引用同一对象。
Equals 用于对引用类型的值相等性进行语义检查。默认情况下,这两个操作是相同的。
Look in the accepted answer for this question: == vs. Object.Equals(object) in .NET
The difference is one of identity equality vs semantic equality. Overriding == is meant for structs that are supposed to be the 'same' because they have the same values, since structs are copied by value and therefore are never references to the same object.
Equals is used for a semantic check of value equality for Reference types. By default, the two operations are the same.
尝试将operator==函数设为虚函数。
Try making the operator== function virtual.