Home > database >  Deep Copy an object values without copying its reference
Deep Copy an object values without copying its reference

Time:05-27

I have an object with lists inside. I need to copy that object value without copying its reference. To explain further, I can edit the original object but the duplicate object should not be altered.

How can i solve this?

CodePudding user response:

This is so simple, No need of using some cloning method and inheriting iclonable and all.

I am using .NET 6.0.

var duplicateObject = new List<ClassName>(OldObject);

This will automatically create a new duplicateObject by deep coping OldObject. By editing the OldObject it doesnot affect the duplicateObject and can be used as backup object or adding some of the removed items back to the OldObject when it is functionally needed.

  • Related