Connecting Tech Pros Worldwide Help | Site Map

copy string array elements to strings in C#

Eranga
Guest
 
Posts: n/a
#1: November 17th, 2005, 06:26 AM


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?

*** Sent via Developersdex http://www.developersdex.com ***
sumit
Guest
 
Posts: n/a
#2: November 17th, 2005, 06:26 AM

re: copy string array elements to strings in C#


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

sumit
Guest
 
Posts: n/a
#3: November 17th, 2005, 06:26 AM

re: copy string array elements to strings in C#


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

Fabien Bezagu
Guest
 
Posts: n/a
#4: November 17th, 2005, 06:26 AM

re: copy string array elements to strings in C#


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]


Fabien Bezagu
Guest
 
Posts: n/a
#5: November 17th, 2005, 06:26 AM

re: copy string array elements to strings in C#


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]


Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#6: November 17th, 2005, 06:28 AM

re: copy string array elements to strings in C#


Eranga <kmm_e@yahoo.com> wrote:[color=blue]
> 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?[/color]

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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#7: November 17th, 2005, 06:28 AM

re: copy string array elements to strings in C#


Eranga <kmm_e@yahoo.com> wrote:[color=blue]
> 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?[/color]

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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#8: November 17th, 2005, 06:28 AM

re: copy string array elements to strings in C#


<"Fabien Bezagu" <fbezagu_at_novacor_dot_fr>> wrote:

<snip>
[color=blue]
> 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[/color]

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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#9: November 17th, 2005, 06:28 AM

re: copy string array elements to strings in C#


<"Fabien Bezagu" <fbezagu_at_novacor_dot_fr>> wrote:

<snip>
[color=blue]
> 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[/color]

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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Fabien Bezagu
Guest
 
Posts: n/a
#10: November 17th, 2005, 06:28 AM

re: copy string array elements to strings in C#


Yes, you're right. I answered too fast. I'm sorry.

Fabien

"Jon Skeet [C# MVP]" <skeet@pobox.com> a écrit dans le message de news:
MPG.1d4b36ba6b5a274398c4eb@msnews.microsoft.com...[color=blue]
> <"Fabien Bezagu" <fbezagu_at_novacor_dot_fr>> wrote:
>
> <snip>
>[color=green]
>> 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[/color]
>
> 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.
>
> --
> Jon Skeet - <skeet@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too[/color]


Fabien Bezagu
Guest
 
Posts: n/a
#11: November 17th, 2005, 06:28 AM

re: copy string array elements to strings in C#


Yes, you're right. I answered too fast. I'm sorry.

Fabien

"Jon Skeet [C# MVP]" <skeet@pobox.com> a écrit dans le message de news:
MPG.1d4b36ba6b5a274398c4eb@msnews.microsoft.com...[color=blue]
> <"Fabien Bezagu" <fbezagu_at_novacor_dot_fr>> wrote:
>
> <snip>
>[color=green]
>> 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[/color]
>
> 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.
>
> --
> Jon Skeet - <skeet@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too[/color]


Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help me with Concatenating strings c answers 21 October 15th, 2006 08:25 AM
copy string array elements to strings in C# Eranga answers 0 November 17th, 2005 06:26 AM
copy long strings in C Patrick answers 44 November 15th, 2005 02:08 AM
Copy String structure "A" to string structure "B" Leo Nunez answers 3 November 14th, 2005 06:54 PM