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".
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
CodePudding user response:
There are several things which come in play:
- Difference between
Equals
and==
operator - String interning (compiler uses the same
string
instance for all compile time constants with the same value). - 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";
CodePudding user response:
The type of your variables is object
, so ==
will be reference equality.
In the string case, both variables refer to the same string "Hello", as .NET interns string literals for performance reasons.
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 string, therefore reference equality returns true.
object obj3 = new object(5);
object obj4 = new object(5);
// obj3 and obj4 refer to different objects (holding the same value), therefore reference equality returns false.
CodePudding user response:
When The ==
operator is used with objects, it compares the references of the objects rather than their contents.
In the case of strings, the ==
operator compares the contents of the strings, not their references. Because the string type in C# is a reference type, the ==
operator has been overridden for the string type to compare the contents of the strings not their references.
On the other hand, when you use the ==
operator to compare two integers (which are value types), it compares the values of the integers rather than their references. In this case, the ==
operator will return true
if the values of the two integers are equal, and false
if they are not.
In your code, obj1
and obj2
are both strings with the same value, so the ==
operator returns true. However, obj3
and obj4
are both integers with the same value, but since the ==
operator compares the references of objects rather than their values, it returns false
.