C#C
C#3y ago
Waffen

❔ Deep clone variable

How to create a complete, isolated copy of a variable to prevent the copy from changing when changed parent variable.

I have tried some variants:
  1. The most easiest variant```csharpvar parent = new Class();var clonedParent = parent;// do some changes with parentif (parent == clonedParent) <-- True```
  2. Variant with `ICloneable` *logic still not changed```csharpclass Person : ICloneable{ public string Name { get; set; } public int Age { get; set; } public object Clone() { return new Person { Name = this.Name, Age = this.Age }; }}````if (parent == clonedParent) <-- True` Still True
  3. A little bit harder variant with `ICloneable````csharpclass myClass : ICloneable{ public string Name { get; set; } public int Age { get; set; } public object Clone() { return this.MemberwiseClone(); }}````if (parent == clonedParent) <-- True` Still True
Do you know any other variants to clone and isolate variable?harold
Was this page helpful?