简体   繁体   English

C#operator ==,StringBuilder.Equals,Object.Equals和Object.ReferenceEquals之间的差异

[英]C# Differences between operator ==, StringBuilder.Equals, Object.Equals and Object.ReferenceEquals

I have a question about Object.Equals and Equals(object) . 我有一个关于Object.EqualsEquals(object) My sample code is below: 我的示例代码如下:

class Program
{
    static void Main(string[] args)
    {
        var sb1 = new StringBuilder("Food");
        var sb2 = new StringBuilder("Food");
        Console.WriteLine(sb1 == sb2);
        Console.WriteLine(sb1.Equals(sb2));
        Console.WriteLine(Object.Equals(sb1, sb2));
        Console.WriteLine(Object.ReferenceEquals(sb1, sb2));
        Console.ReadLine();
    }
}

The output is: 输出是:

False
True
False
False

But as far as I'm concerned Object.Equals(sb1, sb2) internally calls sb1.Equals(sb2) so why does it give two different results? 但就我而言, Object.Equals(sb1, sb2)内部调用sb1.Equals(sb2) ,为什么它会给出两个不同的结果呢?

You are missing another test: 你错过了另一个测试:

Console.WriteLine(sb1.Equals((object)sb2)); // False!

StringBuilder does not override Equals(object) , it overloads it with another Equals(StringBuilder) . StringBuilder不会覆盖Equals(object) ,它会使用另一个Equals(StringBuilder) 重载它。

Object.Equals(object, object) is calling Equals(object) , so the result is false. Object.Equals(object, object)正在调用Equals(object) ,因此结果为false。

You are using 4 different methods of comparison, result in different results: 您正在使用4种不同的比较方法,导致不同的结果:

  • Operator == will by default do a check if the references are equal. 默认情况下,运算符==会检查引用是否相等。 In this case you have two instances, so they do have two different references. 在这种情况下,您有两个实例,因此它们有两个不同的引用。 The behavior of == can overridden by any type (like string has its own method of comparison), but in case of the StringBuilder it is not. ==的行为可以被任何类型覆盖(比如string有自己的比较方法),但是在StringBuilder情况下它不是。
  • Method StringBuilder.Equals(StringBuilder) will compare with another StringBuilder and compares some inside values. 方法StringBuilder.Equals(StringBuilder)将与另一个StringBuilder进行比较并比较一些内部值。 These values are, in your case, the same. 在您的情况下,这些值是相同的。 Strange thing is that StringBuilder does not override the method StringBuilder.Equals(object) to apply the same logic. 奇怪的是, StringBuilder不会覆盖方法StringBuilder.Equals(object)来应用相同的逻辑。
  • Method object.Equals(object, object) will try to call the method .Equals(object) of one of the objects. 方法object.Equals(object, object)将尝试调用其中一个.Equals(object)的方法.Equals(object) In this case: StringBuilder.Equals(object) which, as I said, did not have the logic to compare the values. 在这种情况下: StringBuilder.Equals(object) ,正如我所说,它没有比较值的逻辑。 Resulting in just a compare of the references of both instances. 导致只比较两个实例的引用。
  • Object.ReferenceEquals will just compare the references. Object.ReferenceEquals将只比较引用。

For more info, see: 有关详细信息,请参阅:

StringBuilder.equals does not compare the objects but rather from MSDN : StringBuilder.equals不会比较对象,而是来自MSDN

"True if this instance and sb have equal string , Capacity , and MaxCapacity values; otherwise, false." “如果此实例和sb具有相等的字符串容量MaxCapacity值, 则为True ;否则为false。”

The rest of the checks you're doing compare the reference. 您正在进行的其他检查会比较参考。

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

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