Here’s an example of implementing ICloneable in two custom classes so that you can use the Clone method to do a deep copy.

To do a deep copy of the Apple class, we need to copy its members that are value types and then create a new instance of Color by calling its Clone method.

[snippet slug=class lang=csharp]

The Colorclass uses MemberwiseClone to make a copy of itself.

[snippet slug=clonable2 lang=csharp]

Cloning an Apple:

[snippet slug=clonable3 lang=csharp]

4/5 - (3 votes)