简体   繁体   中英

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

Today, I was reading about == operator, Equal function, and Object.ReferenceEqual method.

  • 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.

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.

According to my knowledge, Here I am doing casting. So I am getting TRUE as == operator check references.

Line 2 – Output is False.

Question 1. Equal function check value of the object. Here we have same value, but I am getting False. WHY?

Line 3 – Output is True.

Question 2. Here I am doing casting, so I am getting True. WHY?

Line 4. Output is False.

According to my knowledge, both the objects are different type. So, I am getting FALSE.

Line 5. Output is False.

Question 3. Here I am doing casting, but still I am getting False. WHY?

Question 4. What is difference between == Operator and Object.ReferenceEqual?

Equal function check value of the object. Here we have same value, but I am getting 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" . String "5" is one of possible representations of the integer value 5 - namely, its decimal representation. 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.

Here I am doing casting, so I am getting True. WHY?

You are not doing casting, you are converting 5 to its decimal string representation, which is "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).

What is difference between == Operator and Object.ReferenceEqual ?

Object.ReferenceEqual ignores possible overloads of operator == .

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. 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. (The whole process of choosing appropriate method is called method resolution)

Line 1: True . Because string.operator== has chosen by compiler and it compares the strings not their references.

Line 2: False . Because int.Equals(object) is chosen by compiler and object ( strValue ) is a string not an int . int is not equal to string so it is false.

Line 3: True . Because string.Equals(string) is chosen by compiler and it checks equality of the values not references.

Line 4: False . Becuase object.ReferenceEquals checks for reference equality so the answer is obvious.

Line 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.

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

Question 2: string.Equals return true if the values are the same, which they are, in both cases: "5". https://msdn.microsoft.com/en-us/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

Question 4: == is the same as referenceequal, except for string. But == operator can be overloaded, so if you want to test identity, use ReferenceEquals https://msdn.microsoft.com/nl-nl/library/6a71f45d.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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