我为什么要在C#实现ICloneable?要在、我为、ICloneable

2023-09-02 01:21:06 作者:﹌丗俗隱маηㄟ

您可以向我解释为什么我要继承 ICloneable 和实施克隆()的方法?

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

如果我想要做一个深拷贝,我不能只是执行我的方法?比方说, MyClone()

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

为什么要我从 ICloneable 继承?有什么优势?它是制作只是一个问题code更具可读性?

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

推荐答案

您不应该。微软建议不要实施 ICloneable ,因为有从接口没有明确标明是否你的克隆方法执行一个深或浅的克隆。

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.

请参阅从布拉德·艾布拉姆斯这个博客帖子早在2003年(!)了解更多信息。

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