Search code examples
c#.netequalityequals-operatorreferenceequals

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?


Solution

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