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