简体   繁体   English

默认 == 运算符实现是否在 C# 内部调用 ReferenceEquals?

[英]Does default == operator implentation calls ReferenceEquals internally in C#?

I know that == operator implentation only do reference check by default, and it doesn't call Object.Equals virtual method.我知道==运算符实现默认只做引用检查,它不调用Object.Equals虚拟方法。 Since Object class also have a ReferenceEquals method that does reference check, so does == operator default implentation calls Object.ReferenceEquals method internally?由于Object class 也有一个ReferenceEquals方法进行引用检查,所以==运算符默认实现在内部调用Object.ReferenceEquals方法

The default implementation of == for a class (not a struct) that has no == operator overload defined, is to do a straight reference comparison with a ceq instruction.对于没有定义==运算符重载的 class(不是结构), ==的默认实现是使用ceq指令进行直接引用比较。 It does not call ReferenceEquals (which cannot be overridden as it is static ).调用ReferenceEquals (不能被覆盖,因为它是static )。

The code for ReferenceEquals is quite simple: ReferenceEquals 的代码非常简单:

public static bool ReferenceEquals (Object objA, Object objB) {
        return objA == objB;
    }

So it's exactly the same thing.所以这完全一样。

  1. == checks for equality ==检查是否相等
  2. ReferenceEquals checks for identity ReferenceEquals检查身份
  3. Identity assumes equality, equality does not assume identity身份假定平等,平等不假定身份
  4. == and Equals on a reference type defaults to a reference check, howevercan be overridden by its actual type, ReferenceEquals cannot引用类型上的==Equals默认为引用检查,但是可以被其实际类型覆盖, ReferenceEquals不能

Going further via the CLI ECMA specs通过 CLI ECMA 规范更进一步

Identity身份

  • If the values have different exact types (cat and dog), then they are not identical.如果值具有不同的确切类型(猫和狗),则它们不相同。
  • If their exact type is a value type, then they are identical if and only if the bit sequences of the values are the same (bit by bit)如果它们的确切类型是值类型,那么当且仅当值的位序列相同(逐位)时,它们才是相同的
  • If their exact type is a reference type, then they are identical if and only if the locations of the values are the same in memory.如果它们的确切类型是引用类型,那么当且仅当值的位置在 memory 中相同时,它们才是相同的。

Equality平等

  • Equality is baked into value types and part of its exact type平等被纳入值类型及其精确类型的一部分
  • Identity should imply equality身份应该意味着平等
  • If either (or both) operand is a boxed value, equality should be computed by如果其中一个(或两个)操作数是装箱值,则应通过以下方式计算相等性
    • first unboxing any boxed operand(s), and then首先拆箱任何装箱的操作数,然后
    • applying the usual rules for equality on the resulting values.对结果值应用通常的相等规则。

Identity is implemented via the ReferenceEquals .身份是通过ReferenceEquals实现的。 Equality is implemented via the Equals method.相等是通过Equals方法实现的。

Some examples一些例子

  • The types Cat and Dog if not in a hierarchical relationship can never be identical.如果不是层次关系,则类型CatDog永远不可能相同。
  • If Cat and Dog are separate instances, they can't be identical.如果CatDog是单独的实例,则它们不能相同。
  • Casting a reference type Cat to Mammal , preserves the identity and are identical将引用类型Cat转换为Mammal ,保留身份并且相同
  • (byte)4 and (int)4 are not identical however they are equivalent (byte)4(int)4不相同,但它们是等价的
  • (byte)4 and (byte)4 are identical and therefore equivalent. (byte)4(byte)4是相同的,因此是等价的。
  • Casting a value type may not preserve identity, however though through coercion may still be equivalent强制转换值类型可能不会保留身份,但是通过强制转换可能仍然是等效的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM