Consider this to :
string s1 = "abcdef";
string s2 = "abc";
s2 += "def";
Console.WriteLine("s1 = s2 ? {0}", s1.Equals(s2));
Console.WriteLine("ref(s1) = ref(s2) ? {0}", object.ReferenceEquals(s1,s2));
and try to change the initialisation of s2 with :
string s2 = "abcdef";
If you are curious, try to open your assembly with ildasm.
-------------
Explanation (well I'll try) :
It's all a matter of reference type and immutable type. String are an
immutable reference type. When assigning "abcdef" to s1, the compiler will
create a local string which reference will be assigned to s1. Then, if
another string needs to be set to "abcdef", the same reference will be
reused to save space.
However, when setting first "abc" and then "def" to s2, two strings are
created locally and thus two references.
Your problems with array are the same.I can reasonably suppose that eranga's
words array comes from elsewhere but sumit created a local array with local
references
I hope it's clear and that helps
Fabien
"sumit" <sumitbhuttan@gmail.com> a écrit dans le message de news:
1122014712.987425.264780@f14g2000cwb.googlegroups. com...[color=blue]
>I tried this code given below & is printing True for all the
> comparisions
>
> string[] words = {"Exclud","Exclud","Exclud","Exclud"};
>
> string test1 = words[2];//"Exclud"
> string test2 ="Exclud";
> string test3 = String.Copy(words[2]);//"Exclud"
>
> bool booTest1 = test1.Equals(test2);//false
> Console.Write(booTest1);
> bool booTest2 = test2.Equals("Exclud");//true
> Console.Write(booTest2);
> bool booTest3 = test1.Equals("Exclud");//false
> Console.Write(booTest3);
> bool booTest4 = words[0].Equals("Exclud");//false
> Console.Write(booTest4);
> bool booTest5 = test3.Equals("Exclud");//false
> Console.Write(booTest5);
> bool booTest6 = test1.Equals(test1); //true
> Console.Write(booTest6);
>[/color]