C# == 运算符详细做什么?
在 C# 中,当您在两个对象上使用“==”运算符进行比较时,后台到底发生了什么? 它只是比较地址吗? 或者是类似 Equals() 或 CompareTo() 的东西吗?
PS:java中的“==”运算符怎么样? 它的行为相同吗?
in c# what does exactly happen in the background when you do a comparison with the "==" operator on two objects? does it just compare the addresses? or does it something like Equals() or CompareTo() ?
PS: what about the "==" operator in java? does it behave the same?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
作为 Stefan 出色答案的扩展 -另一个例外是操作数涉及
Nullable
- 在这种情况下适用“提升”运算符(ECMA 334v4 中的 14.2.7):这意味着:因为(例如)之间存在一个相等运算符:
因此存在一个以下形式的隐式运算符(尽管做法不同):
As an extension to Stefan's excellent answer - another exception is if the operands involve
Nullable<T>
- in which case "lifted" operators apply (14.2.7 in ECMA 334v4):What that means is: because there is an equality operator between (say):
Thus there is an implicit operator of the form (although done differently):
来自 MSDN:
From MSDN:
不... == 运算符在 java 和 c# 中的行为并不总是相同。
例如字符串; Java == 确实比较字符串对象的引用...(如果使用原始类型,java 中的 == 会比较值)。 这就是为什么
在 java 中不会返回 true...
相比之下,在 C# 中, == 运算符在字符串上的行为确实有所不同。 例如,在以下情况下它将返回 true:
No ... the == operator does not always behave the same in java and in c#.
For example with Strings; Java == does compare the references of the string objects... (if you use primitve types, == in java compares the values). That's why
will not return true in java...
In C# in contrast, the == operator does behave different on strings. For example, it will return true in the following case:
它的作用取决于上下文。
http://en.csharp-online.net/ECMA-334: _14.9_Relational_and_type-testing_operators
What it does depends on the context.
http://en.csharp-online.net/ECMA-334:_14.9_Relational_and_type-testing_operators
== 运算符的行为取决于您应用它的变量的声明方式(不是在对象的类上,我将添加一个示例)。
对于值类型,它将比较它们的值。
对于引用类型,如果 a 与 b 是同一对象,则 a == b 返回 true,除非 == 运算符被重载。 不像其他人所说的那样被重写,你不能重写 C# 中的运算符,因为它们不是虚拟的。
<代码>
对象 obj_a, obj_b;
string str_a, str_b;
该程序的输出是
The behavior of == operator depends how the variable you are applying it to was declared (not on the class of the object, I'll add an example).
For value types it will compare their values.
For reference types a == b returns true if a is the same object as b, unless the == operator was overloaded. Not overridden as others said, you can't override operators in c# because they are not virtual.
object obj_a, obj_b;
string str_a, str_b;
The output of that program is
据我所知:
Equals 在对象中实现,也可以被覆盖。 Object 中的默认实现对引用类型执行引用比较。 所以默认情况下,Equals 和 == 的作用是相同的。
我认为在java中你不能重载==运算符。 但我的 Java 知识已经相当过时了。
编辑:
请注意,
==
运算符是静态方法。 它在编译时根据变量或字段的类型进行绑定。Equals
是一个在运行时根据实际运行时类型找到的虚拟方法。As far as I know:
Equals is implemented in object and can be overridden as well. The default implementation in Object performs a reference comparison for reference types. So by default, Equals and == do the same.
I think in java you cannot overload the == operator. But my Java knowledge is pretty outdated.
Edit:
Note that the
==
operator is a static method. It is bound at compile time, base on the types of your variables or fields.Equals
is a virtual method that is found at runtime, based on actual runtime types.