Connecting Tech Pros Worldwide Help | Site Map

copy string array elements to strings in C#

 
LinkBack Thread Tools Search this Thread
  #1  
Old November 17th, 2005, 05:26 AM
Eranga
Guest
 
Posts: n/a
Default copy string array elements to strings in C#



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

  #2  
Old November 17th, 2005, 05:26 AM
sumit
Guest
 
Posts: n/a
Default 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);

  #3  
Old November 17th, 2005, 05:26 AM
sumit
Guest
 
Posts: n/a
Default 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);

  #4  
Old November 17th, 2005, 05:26 AM
Fabien Bezagu
Guest
 
Posts: n/a
Default 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]


  #5  
Old November 17th, 2005, 05:26 AM
Fabien Bezagu
Guest
 
Posts: n/a
Default 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]


  #6  
Old November 17th, 2005, 05:28 AM
Jon Skeet [C# MVP]
Guest
 
Posts: n/a
Default 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
  #7  
Old November 17th, 2005, 05:28 AM
Jon Skeet [C# MVP]
Guest
 
Posts: n/a
Default 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
  #8  
Old November 17th, 2005, 05:28 AM
Jon Skeet [C# MVP]
Guest
 
Posts: n/a
Default 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
  #9  
Old November 17th, 2005, 05:28 AM
Jon Skeet [C# MVP]
Guest
 
Posts: n/a
Default 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
  #10  
Old November 17th, 2005, 05:28 AM
Fabien Bezagu
Guest
 
Posts: n/a
Default 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]


  #11  
Old November 17th, 2005, 05:28 AM
Fabien Bezagu
Guest
 
Posts: n/a
Default 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]


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.