Why is MemberwiseClone defined in System.Object protected?

前端 未结 4 466
礼貌的吻别
礼貌的吻别 2020-12-18 21:42

I\'m wondering why MemberwiseClone is defined as protected. This means that only derived types can access it. What is the problem if it was defined as public?

4条回答
  •  余生分开走
    2020-12-18 22:03

    • A lot of things don't make sense to be cloned; anything that talks to an unmanaged handle, for example
    • Most objects do not need a clone facility
    • Deep-copying something properly is really hard if you go outside of a few simple cases
    • In many cases, there are better metaphors than blind clones
    • Manually adding a clone facility to your types that need it is trivially easy

    To me, then, it is a no-brainer that this should not be added to the public API by default.

提交回复
热议问题