c# – 如果“==运算符未定义”会发生什么?
如果“==运算符未定义”会发生什么?
例: class a { int variable = 0; } class b { void proc() { a ref1 = new a(); a ref2 = new a(); bool cmp1 = ref1 == ref2;//? bool cmp2 = ref1 == ref1;//? } } 使用结构时是否有所不同? 编组(System.Runtime.Remoting.*)对象(单例)怎么样? 解决方法
对于用户定义的值类型,您的代码将无法编译.
具体来说,编译失败会出现以下错误:“运算符’==’不能应用于’a’和’a’类型的操作数.” “The == and != operators cannot operate on a struct unless the struct explicitly overloads them.” 你必须超载both of them.你很可能不想在你的方法中使用默认的Equals(),因为 对于用户定义的引用类型(简化的情况,如在OP的示例中): “The == and != operators can be used with classes even if the class does not overload them. However,the default behavior is to perform a reference equality check. In a class,if you overload the Equals method,you should overload the == and != operators,but it is not required.” 如果不重载运算符,则很可能只有参考相等性测试. “简化案例”,因为operator overload resolution可能会选择另一个实现而不是default. //Minimal example,for demonstration only. //No Equals(),GetHaschode() overload,no IEquatable<T>,null checks,etc.. class Program { static void Main() { MyMoreDerived a = new MyMoreDerived() { fbase = 1,fderived = 3 }; MyMoreDerived b = new MyMoreDerived() { fbase = 2,fderived = 3 }; //Even though MyMoreDerived does not overload the operators,this //will succeed - the definition in MyDerived will be used. if (a == b) { //Reached,because the operator in MyDerived is used. Console.WriteLine("MyDerived operator used: a == b"); } a.fderived = 2; b.fbase = 1; //a => {1,2} //b => {1,3} //Since 2 != 3,the operator in MyDerived would return false. //However only the operator in MyBase will be used. if ((MyBase)a == (MyBase)b) { //Reached,because the operator in MyBase is used. Console.WriteLine("MyBase operator used: a == b"); } b.fderived = 2; //a => {1,2} //Now both operator definitions would compare equal,//however they are not used. if ((object)a != (object)b) { //Reached,because the default implementation is used //and the references are not equal. Console.WriteLine("Default operator used: a != b"); } } class MyBase { public int fbase; public static bool operator ==(MyBase x,MyBase y) { return x.fbase == y.fbase; } public static bool operator !=(MyBase x,MyBase y) { return x.fbase != y.fbase; } } class MyDerived : MyBase { public int fderived; public static bool operator ==(MyDerived x,MyDerived y) { return x.fderived == y.fderived; } public static bool operator !=(MyDerived x,MyDerived y) { return x.fderived != y.fderived; } } class MyMoreDerived : MyDerived { } } 单身人士在引用类型的上下文中最有意义,其目的是返回一个特定的实例.我无法想象一个合理的情况,即引用是相同的但是对象与自身“不相等”. 即使使用远程处理,最好将操作合同与数据合同分开. 即使你提供了一个重载运算符的自定义客户端代理,这也是一个非常糟糕的做法和调试噩梦,隐藏在==和!=运算符后面的远程调用.(如果我理解你的意图,我不确定.) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |