Search code examples
c#.netcloneableicloneable

Why should I implement ICloneable in c#?


Can you explain to me why I should inherit from ICloneable and implement the Clone() method?

If I want to do a deep copy, can't I just implement my method? Let's say MyClone()?

Why should I inherit from ICloneable? What are the advantages? Is it just a matter of making code "more readable"?


Solution

  • You shouldn't. Microsoft recommends against implementing ICloneable because there's no clear indication from the interface whether your Clone method performs a "deep" or "shallow" clone.

    See this blog post from Brad Abrams back in 2003(!) for more information.