Why should I implement ICloneable in c#?

uinc picture uinc · Mar 31, 2009 · Viewed 48.2k times · Source

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"?

Answer

Matt Hamilton picture Matt Hamilton · Mar 31, 2009

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.