Usually C# is a pleasant language to work with—it makes logical sense and can accommodate fairly complex programs. But some features in the language are inconvenient or downright flawed. One feature I like to avoid is the Clone
method.
Part of the ICloneable
interface
, the idea behind Clone
is that we have this interface
that we use to define how an object
is cloned (duplicated). But the interface
does not do that. It does not specify to what extent the object
is cloned. Here are the problems:
Clone
may copy just the top-level fields from the object
, not any nested fields.Clone
calls may be shallow and some deep, we never really know what we are getting when we use Clone
.So ICloneable
is supposed to give us a standard way to duplicate objects, but it just makes us more confused when we cannot figure out what the method does with regards to nested objects. It is a counterproductive interface
—one that makes programs harder to use and more confusing. For the most part, it is best to avoid ICloneable
and Clone
altogether.