True Copying Reference-Value Objects

I have stumbled upon the following problem:

Given an object (any reference-value class) O1, how can I copy it to another object O2.

O1 = O2; //will only copy the reference, but I want a new object being exactly identic to the old one.

Also, how can I do the same thing on an Array

int[] arr1, arr2;

arr1 = arr2;// also, doesn't work, only copies the reference

Hope I made myself clear enough.

Thanks in advanced.



Answer this question

True Copying Reference-Value Objects

  • crowsfoot

    Hi

    have a look at http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=1119209&SiteID=1

    Hope this helps, please close the thread if it does



  • RexH

    Thank you for the answer, but I have another question.

    How do I do a deep copy of ArrayList

    the ArrayList.Clone() creates a shalow copy, and it doesn't create new objects for every object in the array. Is there any way I could create a deep copy of an ArrayList My guess is not, because the ArrayList might contain some objects which do not implement IClonable.


  • ytandeta

    Some objects inherits from the ICloneable interface. You can use the obj.Clone() method to make it. Its also works fine for arrays.

    Regards



  • Andy Burns

    Hi,

    You need to implement it yourself.

    Meaning you need to override the base implementation and give your own "deep" copy.

    Guy kolbis



  • True Copying Reference-Value Objects