Lately a friend asked me why there's no such thing as the clone function in C#. But I don't really get why to use clone except of this:
SomeClass cls = new SomeClass();
SomeClass cls2 = cls;
Why would you use = cls.clone();, so like this:
SomeClass cls = new SomeClass();
SomeClass cls2 = (SomeClass) cls.clone();
I never understood the difference. Can anyone explain which one is better and what the difference is?
Best How To :
What you have done here
SomeClass cls = new SomeClass();
SomeClass cls2 = cls;
Was creating TWO referencec to ONE, same object. Picture (may be a bit ugly because I drew it myself ;)):

In this code however:
SomeClass cls = new SomeClass();
SomeClass cls2 = (SomeClass) cls.clone();
You created TWO Objects with TWO references, where the second object is a copy of the first object. cls points to the first object, whilst cls2 points to the second one. Jvm will have to reserve space for two different objects instead of just one. Picture:
