-----------------------------------------------------------

New Message on BDOTNET

-----------------------------------------------------------
From: Roark
Message 4 in Discussion

The example u have tried out is a perfect example of shallow copy. What has happened 
is that testClone, o1 and o2 all refer to the same object. So changing any one of them 
will result in manupulating the same object. This is because u have clonned the 
arraylist and not TestClone.    I made a goof-up in the first reply i gave. The string 
class is immutable and any changes made to it will result in a new string bieng 
returned. If u substitute the Stringbuilder for the string class in the explanation i 
gave earlier it should be fine.   Now try this: add the integer and the string 
directly to the arrylist. also add a string builder. Then clone the arrylist. now if u 
modify the stringBuilder object from any of the arraylists or the original 
StringBuilder reference, the change will be reflected everywhere. But for the string 
and the interger this is not true.    
ArrayList arr = new ArrayList(); 
int x = 10; 
string str = "abc"; 
StringBuilder SB = new StringBuilder("abc"); 
arr.Add(x); 
arr.Add(str); 
arr.Add(SB); 
ArrayList arr2 = (ArrayList) arr.Clone(); 
arr[0] = (int)arr[0]+9;   
((string)arr[1]).Insert(0,"AppendToString");    
((StringBuilder)arr[2]).Insert(0,"AppendToBuilder"); 
//or  
//((StringBuilder)arr2[2]).Insert(0,"AppendToBuilder"); 
//or 
//SB.Insert(0,"AppendToBuilder"); 
//only changes made to the stringbuilder object get reflected in both places 
//the changes made to the string do not as it is an immutable object 
Console.WriteLine(""+arr[0]+arr[1]+arr[2]); 
Console.WriteLine(""+arr2[0]+arr2[1]+arr2[2]);  

-----------------------------------------------------------

To stop getting this e-mail, or change how often it arrives, go to your E-mail 
Settings.
http://groups.msn.com/bdotnet/_emailsettings.msnw

Need help? If you've forgotten your password, please go to Passport Member Services.
http://groups.msn.com/_passportredir.msnw?ppmprop=help

For other questions or feedback, go to our Contact Us page.
http://groups.msn.com/contact

If you do not want to receive future e-mail from this MSN group, or if you received 
this message by mistake, please click the "Remove" link below. On the pre-addressed 
e-mail message that opens, simply click "Send". Your e-mail address will be deleted 
from this group's mailing list.
mailto:[EMAIL PROTECTED]

Reply via email to