gpt4 book ai didi

c# - 如果 "== operator is not defined"会发生什么?

转载 作者:行者123 更新时间:2023-11-30 14:19:37 25 4
gpt4 key购买 nike

如果“== operator is not defined”会怎样?

例子:

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.*) 对象(单例)怎么样?

最佳答案

对于用户定义的值类型,您的代码将无法编译。

具体来说,它会因以下错误而导致编译失败:“Operator '==' cannot be applied to operands of type 'a' and 'a'”。

"The == and != operators cannot operate on a struct unless the struct explicitly overloads them."

你必须重载 both of them .您很可能不想在您的方法中使用默认的 Equals(),因为"...对于结构,Object.Equals(Object) 的默认实现(它是 System.ValueType 中的重写版本)通过使用反射来比较类型中每个字段的值来执行值相等性检查。当实现者覆盖结构中的虚拟 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}
//b => {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
{
}

}

单例在引用类型的上下文中最有意义,它们的目的是返回一个特定的实例。我无法想象引用相同但对象不“等于”自身的合理情况。

即使使用远程处理,最好的做法是将操作契约(Contract)与数据契约(Contract)分开。前者通常由服务器端的 MarshalByRefObject 实现 - 实现由接口(interface)定义的操作 - 而后者具有按值编码并可能由客户端和服务器共享的数据/消息类。如果您在数据类中重载运算符,这可能不是什么大问题。但是,我相信这些不应该引用/调用远程对象。

即使您提供了一个使运算符过载的自定义客户端代理,恕我直言,将远程调用隐藏在 ==!= 后面是一个非常糟糕的做法和调试噩梦> 运营商。(如果我理解你的意图,我不确定。)

关于c# - 如果 "== operator is not defined"会发生什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2483238/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com