WHAT IS THE PURPOSE OF CLONE() IN C#?

Takeshi Okada

Total Post:89

Points:629
Posted by  Takeshi Okada
C# 
C#
 1672  View(s)
Ratings:
Rate this:
Hi Expert!
 i did not understand, what is the purpose of Clon() method in  objects cloning;
thanks in advance!
  1. shreesh chandra shukla

    Post:105

    Points:735
    Re: What is the purpose of Clone() in C#?

    Hi!

    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.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!