I have these statements and their\' results are near them.
string a = \"abc\";
string b = \"abc\";
Console.Writeline(a == b); //true
object x = a;
object y
Because they are two different object references. The built-in comparison for this is to compare whether they point to the same actual object or not.
Due to String Interning, a
and b
are both references to the same string object.
c==d
is true because the string equality operator is being used.
As seen on C# FAQ on MSDN - the compiler cannot use the overloaded method and falls back to comparing the references.
The bigger question is why it succeeds in the first object comparison. My best guess is that succeeds because a and b are both given the same reference. For c and d, you are forcing different references.
String
has overloaded the equality operator so that you can use ==
for value comparison. Hence
a == b //true
.
When you're downcasting them to object, you're only comparing references.
String
looks into an internal string-pool if another string instance is already available, otherwise a new instance will be created and added to the pool. So actually a
, b
, x
and y
are even the same reference, that's why
x == y //true
.
With using the constructor of String
, you're forcing .NET to create a new instance even if another string with the same value(length and character sequence) exists. That's why
k == m //false
http://en.csharp-online.net/CSharp_String_Theory%E2%80%94String_intern_pool
In the case of strings, the ==
operator is overloaded to test for value equality, when using object
reference equality is used.
Since c
and d
are strings, when you use Equals
in the k
and m
, the overloaded method is used.
And c == d
is true
for the reason state above - value equality is used on string
types as the operator is overloaded.
When you say string1 == string2
, the comparison uses the string
type's overloaded ==
operator, which compares the strings' values.
When you say object1 == object2
, even though they're strings in this case, they won't qualify as strings for the purposes of finding an operator. So the comparison uses the default ==
operator, which compares the references for equality. Which means, if the two objects aren't the exact same object, it will return false.
Building on Bob2Chiv's, I tried the equivalent in VB (VS2010):
Dim a As String = "abc"
Dim b As String = "abc"
Console.WriteLine(a = b) ' True
Dim x As Object = a
Dim y As Object = b
Console.WriteLine(x = y) ' True
Dim c As String = New String(New Char() {"a"c, "b"c, "c"c})
Dim d As String = New String(New Char() {"a"c, "b"c, "c"c})
Console.WriteLine(c = d) ' True
Dim k As Object = c
Dim m As Object = d
Console.WriteLine(k.Equals(m)) ' True
Console.WriteLine(k = m) ' True (Yes, True!!)
Console.WriteLine(k Is m) ' False (Like in C#)
Console.WriteLine(a Is b) ' True (Like in C#)
(At least I think it's the equivalent - I welcome being corrected on this.)
Moral?: Be careful of ==
in C# - prefer .Equals()
when the comparison of values is what's desired?