Home
Blog
This Is My Least Favorite C# Feature
Updated
Dot Net Perls

This Is My Least Favorite C# Feature

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.
A developer may implement a clone that does "deep" copies if this is desired—there are no rules here.
Because some 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.

Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
An RSS feed is available for this blog.
Home
Changes
© 2007-2025 Sam Allen