简体   繁体   English

整数对象和字符串对象的 C# 相等运算符以不同方式工作

[英]C# equality operator for Integer Object and String Object working differently

In C# object, When I compare 2 string's (same value) with equality operator I getting result as "True" but whereas if I compare 2 Integer's (same value) with equality operator I'm getting "False".在 C# 对象中,当我将 2 个字符串(相同值)与相等运算符进行比较时,我得到的结果为“真”,但是如果我将 2 个整数(相同值)与相等运算符进行比较,我得到的结果是“假”。

Can anyone explain me how this thing works?谁能解释一下这东西是如何工作的?

using ConsoleApp1;

object obj1 = "Hello";
object obj2 = "Hello";


object obj3 = 5;
object obj4 = 5;

Console.WriteLine(obj1 == obj2); // True
Console.WriteLine(obj1.Equals(obj2)); // True

Console.WriteLine(obj3 == obj4); // False // This should be true right?
Console.WriteLine(obj3.Equals(obj4)); // True

There are several things which come in play:有几件事在起作用:

  1. Difference between Equals and == operator Equals==运算符之间的区别
  2. String interning (compiler uses the same string instance for all compile time constants with the same value).字符串实习(编译器对具有相同值的所有编译时常量使用相同的string实例)。
  3. Boxing of value types (see also value types and reference types article) 值类型装箱(另请参阅值类型和引用类型文章)
object obj1 = "Hello";
object obj2 = "Hello";
object obj2_2 = "Hell" + getO(); // "Hello", but different instance

object obj3 = 5; // boxed to one instance with value 5
object obj4 = 5; // boxed to second instance with value 5

Console.WriteLine(obj1 == obj2); // True
Console.WriteLine(obj1.Equals(obj2)); // True

Console.WriteLine(obj1 == obj2_2); // False
Console.WriteLine(obj1.Equals(obj2_2)); // True

Console.WriteLine(obj3 == obj4); // False // This should be true right?
Console.WriteLine(obj3.Equals(obj4)); // True

string getO() => "o";

The type of your variables is object , so == will be reference equality.你的变量的类型是object ,所以==将是引用相等性。

In the string case, both variables refer to the same string "Hello", as .NET interns string literals for performance reasons.在字符串情况下,两个变量都引用相同的字符串“Hello”,因为出于性能原因,.NET 实习生字符串文字。

In the integer case, each variable is a different boxed object around the value type integer.在整数情况下,每个变量都是围绕值类型整数的不同装箱对象。 Therefore, reference equality is false.因此,引用相等是错误的。

Here is code illustrating what's going on behind the scenes:这是说明幕后发生的事情的代码:

string helloString = "hello";
object obj1 = helloString;
object obj2 = helloString;

// Both obj1 and obj2 point to the same object, therefore reference equality returns true.

object obj3 = new object(5); // not a real constructor of System.Object, just for illustration
object obj4 = new object(5);

// obj3 and obj4 refer to different objects (holding the same value), therefore reference equality returns false.

But then why does obj3.Equals(obj4) returns true?但是为什么obj3.Equals(obj4)返回 true 呢? Because Equals is a virtual method , while operator== is a static method .因为Equals虚方法,而operator==静态方法

When you call Equals , even if it's on a variable of type object , the actual method that will be called is Int32.Equals , which compares integers by value.当您调用Equals时,即使它是在类型为object的变量上,实际调用的方法是Int32.Equals ,它按值比较整数。

When you call == , the operator getting called is from object , which uses reference equality.当您调用==时,被调用的运算符来自object ,它使用引用相等性。

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

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