See this code:
object x = "mehdi emrani"; string y = "mehdi emrani"; Console.WriteLine(y == x);
But this code:
object x = "mehdi emrani"; string y = "mehdi "; y += "emrani"; Console.WriteLine(y == x);
So when I compare String and Object in first code I get
But when I compare them in second code I get
Both strings are same but why when I append to the string, my result returns
In each case, the second operand of
x, which is of type
object. That means you’re using the normal reference equality operator.
Now in your first case, you’re using two string constants with the same contents. The C# compiler will use a single object for those two references. In the second case,
y refer to distinct string objects with the same contents. The two references will be different, so
== will return false.
You can fix the comparison by:
Equalsinstead – that’s overridden by
string(as opposed to the
==operator which is only overloaded:
Console.WriteLine(y.Equals(x)); // or x.Equals(y), or Equals(y, x)
The use of the static
Equals(object, object)method can be useful if either of the arguments can be null; it means you don’t need to worry about a
Make both variables of type
string, at which point the
stringwill be picked at compile-time, and that overload compares the contents of the strings, not just the references
It’s worth noting that it’s not just a matter of the string literals itself being noticed by the C# compiler – it’s about compile-time constant expressions. So for example:
object x = "mehdi emrani"; string y = "mehdi " + "emrani"; Console.WriteLine(y == x); // True
y is initialized using two string literals which aren’t the same as the one used to initialize
x, but the string concatenation is performed by the compiler, which realizes it’s the same string it’s already used for
When you initialized
object x = "mehdi emrani"; //pointer(x)
It initialized it in memory and assign reference to x. After this when you initialized
string y = "mehdi emrani"; //pointer(x)
compiler find that this value is already in memory so it assign same reference to y.
== equal operator which actually compares the addresses instead of value find the same address for both variable which results true:
x==y //actually compares pointer(x)==pointer(x) which is true
In second case when you initialized x and y that get assigned different addresses.
object x = "mehdi emrani"; //Pointer(x) string y = "mehdi "; //not found in memory y += "emrani"; //Pointer(y)
Now comparison find different addresses which results false:
x == y //is actually Pointer(x) == Pointer(y) which is false
So to overcome this you need to use .Equals() which instead of reference compares the value and object type.
Console.WriteLine(y.Equals(x)); //compares "mehdi emrani" == "mehdi emrani" results true
Most probably the references are compared (standard
Equals implementation for object). In the first example C# optimizes constant strings, and so y and x actually point to the same object, hence their reference is equal. In the other case, y is created dynamically, and so the reference is different.
In the background a new string is created every time you modify an existing one, because strings are immutable, which means they can’t change.
See the following for an explanation: Why .NET String is immutable?
In the first case, .NET performs string constant optimization and allocates only one String instance. Both x and y points to same object (both the references are equal).
But in the second case x and y are pointing to different String instances. Adding “ermani” to y creates a third string object.
“==” operator basically returns true if operands on both sides refer to same object. In the first case x and y refer to same object and in the seconds case x & y refer different objects.
Have you tried:
Console.WriteLine(y == x.ToString());