compare for string eauality

T

Tony Johansson

Hi!

Here I have some strings that is comparing for equality.
I do not fully understand this. I have 4 different checks.
At the number marked with 1 I get true when comparing
At the number marked with 2 I get false when comparing
At the number marked with 3 I get true when comparing
At the number marked with 4 I get false when comparing

My comment about number 1 is the following
Even though string are reference types you comapre the value not the
reference so
here because both is refering to th esame string which is hello you get
true.

My comment about number 2 is the following
In number 2 you compare the reference not the value the reference a is not
the same reference as b so you get false.

My comment about number 3 is the following
Because you have the same string in the string pool the runtime
automatically use the same reference even if if it doesn't look like that.

My comment about number 4 is the following
string are immutable they can't be changed. So in this case the runtime
allocate a new string so the reference is now not the same which give the
result false

Can somebody give some comment about my conclusions ?

static void Main()
{
string a = "hello";
string b = "h";
// Append to contents of 'b'
b += "ello";
1 Console.WriteLine(a == b); // give true
2 Console.WriteLine((object)a == (object)b); //give false

String s1 = "hej";
String s2 = "hej";
3 Console.WriteLine(((object)s1) == (object)s2); //give true

String s3 = "hej";
String s4 = "he";
s4 += "j";
4 Console.WriteLine(((object)s3) == (object)s4); //give false
}

//Tony
 
J

Jeff Johnson

Can somebody give some comment about my conclusions ?

You're correct, but #2 and #4 are the same thing. In both cases you've got
identical string values but different strings in the string table due to
concatenation.
 
A

Arne Vajhøj

Here I have some strings that is comparing for equality.
I do not fully understand this. I have 4 different checks.
At the number marked with 1 I get true when comparing
At the number marked with 2 I get false when comparing
At the number marked with 3 I get true when comparing
At the number marked with 4 I get false when comparing

My comment about number 1 is the following
Even though string are reference types you comapre the value not the
reference so
here because both is refering to the same string which is hello you get
true.

String has overriden the comparison to compare values not
object identity.
My comment about number 2 is the following
In number 2 you compare the reference not the value the reference a is not
the same reference as b so you get false.

Here you are using object comparison which is object identity.
My comment about number 3 is the following
Because you have the same string in the string pool the runtime
automatically use the same reference even if if it doesn't look like that.
Exactly.

My comment about number 4 is the following
string are immutable they can't be changed. So in this case the runtime
allocate a new string so the reference is now not the same which give the
result false

Strings are immutable, but even if they were not then

s = s + "j";

would still create new object.
Can somebody give some comment about my conclusions ?

static void Main()
{
string a = "hello";
string b = "h";
// Append to contents of 'b'
b += "ello";
1 Console.WriteLine(a == b); // give true
2 Console.WriteLine((object)a == (object)b); //give false

String s1 = "hej";
String s2 = "hej";
3 Console.WriteLine(((object)s1) == (object)s2); //give true

String s3 = "hej";
String s4 = "he";
s4 += "j";
4 Console.WriteLine(((object)s3) == (object)s4); //give false
}

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top