C#:对象的 == 和 != 运算符的默认实现

发布于 2024-12-03 16:10:05 字数 293 浏览 0 评论 0原文

我想知道相等运算符(== 和 !=)的默认实现

是什么?

public static bool operator ==(object obj1, object obj2)
{
    return obj1.Equals(obj2);
}
public static bool operator !=(object obj1, object obj2)
{
    return !obj1.Equals(obj2);
}

所以我只需要重写 Equals 方法,还是还需要重写等性运算符?

I'd like to know what is default implementation for equality operatort (== and !=)

Is it?

public static bool operator ==(object obj1, object obj2)
{
    return obj1.Equals(obj2);
}
public static bool operator !=(object obj1, object obj2)
{
    return !obj1.Equals(obj2);
}

So I only need to override Equals method or do I need to override euality operators as well ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一曲爱恨情仇 2024-12-10 16:10:05

不,这不是 - 默认情况下,引用会被检查是否相等。 == 等运算符不是多态的,默认情况下不会调用任何多态的内容。例如:

string x = "Hello";
string y = new String("Hello".ToCharArray());
Console.WriteLine(x == y); // True; uses overloaded operator

object a = x;
object b = y;
Console.WriteLine(a == b); // False; uses default implementation

您不能覆盖相等运算符,但您可以重载它们,就像字符串一样。您是否应该是另一回事。我想如果我重写Equals,我通常会这样做,但不一定总是这样。

No, it's not that - by default, references are checked for equality. Operators such as == are not polymorphic and don't call anything polymorphic by default. So for example:

string x = "Hello";
string y = new String("Hello".ToCharArray());
Console.WriteLine(x == y); // True; uses overloaded operator

object a = x;
object b = y;
Console.WriteLine(a == b); // False; uses default implementation

You can't override equality operators, but you can overload them, as string does. Whether or not you should is a different matter. I think I usually would if I were overriding Equals, but not necessarily always.

甜柠檬 2024-12-10 16:10:05

C# 语言规范,第 7.9 节介绍了内置 == 运算符的确切行为。例如,当对引用类型值使用 == 时,以下部分适用:

7.9.6 引用类型相等运算符

预定义的引用类型相等运算符是:

布尔运算符 ==(对象 x, 对象 y);
布尔运算符 !=(对象 x, 对象 y);

运算符返回比较两个引用相等或不相等的结果。

由于预定义的引用类型相等运算符接受对象类型的操作数,因此它们适用于未声明适用的运算符 == 和运算符 != 成员的所有类型。相反,任何适用的用户定义的相等运算符都会有效地隐藏预定义的引用类型相等运算符。

[跳过更多详细信息...]

请注意,“比较两个引用是否相等”并不意味着“调用 obj1.Equals(obj2) 的结果” em>”。这意味着两个引用必须指向同一个对象(引用相等)。

The C# language specification, Section 7.9 covers the exact behavior of the built-in == operator. For example, when using == on reference-type values, the following section applies:

7.9.6 Reference type equality operators

The predefined reference type equality operators are:

bool operator ==(object x, object y);
bool operator !=(object x, object y);

The operators return the result of comparing the two references for equality or non-equality.

Since the predefined reference type equality operators accept operands of type object, they apply to all types that do not declare applicable operator == and operator != members. Conversely, any applicable user-defined equality operators effectively hide the predefined reference type equality operators.

[More details skipped...]

Note that "comparing two references for equality" does not mean "the result of calling obj1.Equals(obj2)". It means that both references must point to the same object (reference equality).

嘿哥们儿 2024-12-10 16:10:05

默认情况下,这些运算符测试引用是否相等。

By default, those operators test for equality of reference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文