class MyClass : ICloneable { public int Value { get; set; } public object Clone() { return new MyClass { Value = this.Value }; } } MyClass obj1 = new MyClass { Value = 42 }; MyClass obj2 = (MyClass)obj1.Clone(); // creates a new copy of obj1
class MyClass : ICloneable { public int Value { get; set; } public object Clone() { return this.MemberwiseClone(); } } MyClass obj1 = new MyClass { Value = 42 }; MyClass obj2 = (MyClass)obj1.Clone(); // creates a new copy of obj1The Clone() method is part of the System.Object class and therefore does not require any additional package or library.