简体   繁体   English

运算符==,等于方法和C#中的Object.ReferenceEqual

[英]Operator ==, Equal Method and Object.ReferenceEqual in C#

Today, I was reading about == operator, Equal function, and Object.ReferenceEqual method. 今天,我正在阅读==运算符,Equal函数和Object.ReferenceEqual方法。

  • Operator == It is used for check equal references. 运算符==用于检查相等引用。
  • Equal Method - It is used for check equal values. 相等方法-用于检查相等值。
  • Object.ReferencEqual – It is used for check equal references. Object.ReferencEqual –用于检查相等引用。

I have created a test console application. 我已经创建了一个测试控制台应用程序。 I have few questions. 我有几个问题。 It would be helpful for me if you give me all the answers. 如果您给我所有的答案,对我会有所帮助。

class Program
{
    static void Main(string[] args)
    {

        int intValue = 5;
        string strValue = "5";
        Console.WriteLine(string.Format("{0}  ", intValue.ToString() == strValue));// Output is TRUE
        Console.WriteLine(string.Format("{0}  ", intValue.Equals(strValue))); // Output is FALSE
        Console.WriteLine(string.Format("{0}  ", intValue.ToString().Equals(strValue))); // Output is TRUE 
        Console.WriteLine(string.Format("{0}  ", object.ReferenceEquals(intValue, strValue)));// Output is FALSE
        Console.WriteLine(string.Format("{0}  ", object.ReferenceEquals(intValue.ToString(), strValue)));// Output is FALSE


        Console.Read(); 
    }

I have five lines in the Output. 我在输出中有五行。

Line 1 – Output is True. 第1行–输出为True。

According to my knowledge, Here I am doing casting. 据我所知,我正在这里进行铸造。 So I am getting TRUE as == operator check references. 因此,我将TRUE作为==运算符检查引用。

Line 2 – Output is False. 第2行–输出为False。

Question 1. Equal function check value of the object. 问题1.对象的相等功能检查值。 Here we have same value, but I am getting False. 在这里,我们具有相同的值,但是我得到了False。 WHY? 为什么?

Line 3 – Output is True. 第3行–输出为True。

Question 2. Here I am doing casting, so I am getting True. 问题2:我在这里进行投射,所以我的想法是对的。 WHY? 为什么?

Line 4. Output is False. 第4行。输出为False。

According to my knowledge, both the objects are different type. 据我所知,这两个对象都是不同的类型。 So, I am getting FALSE. 所以,我越来越假。

Line 5. Output is False. 第5行。输出为False。

Question 3. Here I am doing casting, but still I am getting False. 问题3:我在这里进行投射,但仍然出现错误。 WHY? 为什么?

Question 4. What is difference between == Operator and Object.ReferenceEqual? 问题4. ==运算符和Object.ReferenceEqual之间有什么区别?

Equal function check value of the object. 对象的相等功能检查值。 Here we have same value, but I am getting False. 在这里,我们具有相同的值,但是我得到了False。 WHY? 为什么?

Objects of different type cannot possibly have the same value. 不同类型的对象不可能具有相同的值。 Your first object has an integer value of 5, while your second object has a string value "5" . 您的第一个对象的整数值为5,而第二个对象的字符串值为"5" String "5" is one of possible representations of the integer value 5 - namely, its decimal representation. 字符串"5"是整数值5的可能表示形式之一,即其十进制表示形式。 However, it is not the same value. 但是,它不是相同的值。 After all, you wouldn't expect "101" to be equal to 5, even though 101 is a binary representation of 5. 毕竟,即使101是5的二进制表示形式,您也不希望"101"等于5。

Here I am doing casting, so I am getting True. 我在这里进行投射,所以我得到了True。 WHY? 为什么?

You are not doing casting, you are converting 5 to its decimal string representation, which is "5" . 您不执行强制转换,而是将5转换为其十进制字符串表示形式,即"5"

Here I am doing casting, but still I am getting False. 在这里,我正在执行转换,但仍然会出错。 WHY? 为什么?

Because you get a different object with the same content (namely, a single character string with character "5" in it). 因为您获得了具有相同内容的另一个对象(即,其中包含字符"5"的单个字符串)。

What is difference between == Operator and Object.ReferenceEqual ? ==运算符和Object.ReferenceEqual之间有什么区别?

Object.ReferenceEqual ignores possible overloads of operator == . Object.ReferenceEqual忽略运算符==可能重载。

Please clear your mind from these statements: 请从以下声明中清除思路:

Operator == It is used for check equal references. 运算符==用于检查相等引用。
Equal Method - It is used for check equal values. 相等方法-用于检查相等值。

Both operator== and Equals can be overridden to modify their behavior. 可以重写operator==Equals来修改其行为。 Since operator== is not virtual it is known at compile time that which method is chosen but Equals is chosen at runtime base on object type. 由于operator==不是虚拟的,因此在编译时知道选择了哪种方法,但是在运行时根据对象类型选择了Equals (The whole process of choosing appropriate method is called method resolution) (选择适当方法的整个过程称为方法解析)

Line 1: True . 第1行:True Because string.operator== has chosen by compiler and it compares the strings not their references. 因为string.operator==已由编译器选择,并且它比较字符串而不是它们的引用。

Line 2: False . 第2行:False Because int.Equals(object) is chosen by compiler and object ( strValue ) is a string not an int . 因为int.Equals(object)由编译器选择,并且objectstrValue )是一个字符串,而不是int int is not equal to string so it is false. int不等于string因此为false。

Line 3: True . 第3行:是 Because string.Equals(string) is chosen by compiler and it checks equality of the values not references. 因为string.Equals(string)是由编译器选择的,并且它检查未引用的值的相等性。

Line 4: False . 第4行:False Becuase object.ReferenceEquals checks for reference equality so the answer is obvious. 因为object.ReferenceEquals检查引用相等性,所以答案很明显。

Line 5: False . 第5行:False Same as above. 同上。 intValue.ToString() creates a new string object on memory with value of "5" and it is not the same object as strValue points to so the answer is false. intValue.ToString()在内存中创建一个值为“ 5”的新字符串对象,它与strValue指向的对象不同,因此答案为假。

Question 1: It only returns true if the type is of Int32, but it's a string https://msdn.microsoft.com/en-us/library/de3wwa8z(v=vs.110).aspx 问题1:仅当类型为Int32时,它才返回true,但这是字符串https://msdn.microsoft.com/zh-cn/library/de3wwa8z(v=vs.110).aspx

Question 2: string.Equals return true if the values are the same, which they are, in both cases: "5". 问题2:string.Equals如果两个值相同,则返回true,在两种情况下均为“ 5”。 https://msdn.microsoft.com/en-us/library/system.string.equals(v=vs.110).aspx https://msdn.microsoft.com/zh-CN/library/system.string.equals(v=vs.110).aspx

Question 3: They point to different objects, hence ReferenceEqual returns false https://msdn.microsoft.com/nl-nl/library/system.object.referenceequals(v=vs.110).aspx 问题3:它们指向不同的对象,因此ReferenceEqual返回false https://msdn.microsoft.com/nl-nl/library/system.object.referenceequals(v=vs.110).aspx

Question 4: == is the same as referenceequal, except for string. 问题4:==与referenceequal相同,除了字符串。 But == operator can be overloaded, so if you want to test identity, use ReferenceEquals https://msdn.microsoft.com/nl-nl/library/6a71f45d.aspx 但是==运算符可以重载,因此,如果要测试身份,请使用ReferenceEquals https://msdn.microsoft.com/nl-nl/library/6a71f45d.aspx

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

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