Cloning objects in C#

Posted by Alison on Stack Overflow See other posts from Stack Overflow or by Alison
Published on 2008-09-17T00:06:27Z Indexed on 2010/05/23 23:10 UTC
Read the original article Hit count: 372

Filed under:
|
|

I want to do something like...

myObject myObj = GetmyObj()//create and fill a new object
myObject newObj = myObj.Clone();

...and then make changes to the new object that are not reflected in the original object.

I don't often need this functionality so when it's been necessary I've resorted to creating a new object and then copying each property individually but it always leaves me with the feeling that there is a better/more elegant way of handling the situation.

How can I clone/deep copy an object so that the cloned object can be modified without any changes being reflected in the original object?

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET