Advertise with us

What is the purpose of Clone() in C#?

Posted by  Takeshi Okada
 2429  View(s)
Rate this:
Hi Expert!
 i did not understand, what is the purpose of Clon() method in  objects cloning;
thanks in advance!
  1. Re: What is the purpose of Clone() in C#?


    In object cloning you can create new same copy of object of same class type. The invoking object without knowing the type in advance.

    For example:

    void Test(ICloneable original)
        var myCloned = original.Clone();

    Here cloned is of the same runtime type as original, and you did not need to know what that type was to perform the cloning.

    However the usefulness of ICloneable is pretty much none, because it does not define the semantics of the clone operation: is it a shallow copy or a deep copy? Since the interface does not mandate one or the other, you can't really know what you are getting back. And since knowing that is essential because you need to handle the clone accordingly, ICloneable itself is pretty much a burned card.

      Modified On Mar-30-2018 05:45:48 AM