copy string array elements to strings in C#

E

Eranga

I have the following code;
string test1 = words[2];//"Exclud"
string test2 ="Exclud";
string test3 = String.Copy(words[2]);//"Exclud"

bool booTest1 = test1.Equals(test2);//false
bool booTest2 = test2.Equals("Exclud");//true
bool booTest3 = test1.Equals("Exclud");//false
bool booTest4 = words[0].Equals("Exclud");//false
bool booTest5 = test3.Equals("Exclud");//false
bool booTest6 = test1.Equals(test1); //true

Though test1 = "Exclud" which is copied from string arrray words[],
when compared it shows that test1 != "Exclud"

Can some one please help me with this?
 
S

sumit

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);
 
F

Fabien Bezagu

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
 
J

Jon Skeet [C# MVP]

Eranga said:
I have the following code;
string test1 = words[2];//"Exclud"
string test2 ="Exclud";
string test3 = String.Copy(words[2]);//"Exclud"

bool booTest1 = test1.Equals(test2);//false
bool booTest2 = test2.Equals("Exclud");//true
bool booTest3 = test1.Equals("Exclud");//false
bool booTest4 = words[0].Equals("Exclud");//false
bool booTest5 = test3.Equals("Exclud");//false
bool booTest6 = test1.Equals(test1); //true

Though test1 = "Exclud" which is copied from string arrray words[],
when compared it shows that test1 != "Exclud"

Can some one please help me with this?

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

It looks to me like neither words[0] nor words[2] is actually equal to
"Exclud", which would explain all your observations.
 
J

Jon Skeet [C# MVP]

<"Fabien Bezagu" <fbezagu_at_novacor_dot_fr>> wrote:

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 can't see how that would change anything. Nowhere in the OP's code or
in sumit's code is reference equality used. All tests are explicitly
using the Equals method.
 

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