I am trying following code:
public class cloneTest : ICloneable {
public string strValue { get; set; }
public object Clone( ) {
cloneTest n = new cloneTest( );
n.strValue = this.strValue;
return n;
}
}
cloneTest obj1 = new cloneTest( ) { strValue = "one" };
cloneTest obj2 = new cloneTest( ) { strValue = "two" };
cloneTest obj3 = new cloneTest( ) { strValue = "three" };
cloneTest[ ] strValueArray = new cloneTest[ ] {obj1, obj2, obj3};
cloneTest[ ] strValueArrayClone = ( cloneTest[ ] )strValueArray.Clone( );
strValueArrayClone[ 2 ].strValue = "four";
When I modify the strValuArrayClone object as specified in my code, this change also reflects in strValueArray object even though, I am creating clone. However if try below code, then everything work smoothly. I want to understand the logic behind it.
cloneTest obj1 = new cloneTest( ) { strValue = "one" };
cloneTest obj2 = new cloneTest( ) { strValue = "two" };
cloneTest obj3 = new cloneTest( ) { strValue = "three" };
cloneTest[ ] strValueArray = new cloneTest[ ] {obj1, obj2, obj3};
cloneTest[ ] strValueArrayClone = ( cloneTest[ ] )strValueArray.Clone( );
cloneTest obj2clone = ( cloneTest )obj2.Clone( );
obj2clone.strValue = "six";
strValueArrayClone[ 2 ] = obj2clone;