> Hi,
> I've a query in cloning. How cloning is different from creating a
> new
> instance of an object.? I suppose cloning also creates a new object and
> copies the exisitng object's data. Where and when should use cloning????//
You're right cloning an object creates a new instance of the class with the
same data as the existing instance.
There are two ways of cloning an object. You can create a shallow copy of an
object by calling the MemberwiseClone method inherited from System.Object. A
shallow copy creates a new instance of the same type as the original object,
and then copies the nonstatic fields of the original object. If the field is
a value type, a bit-by-bit copy of the field is performed. If the field is a
reference type, the reference is copied but the referred object is not;
therefore, the reference in the original object and the reference in the
clone point to the same object.
If the object implements the System.IClonable interface you can call the
Clone method to clone the object. Classes that implement IClonable control
how the instance is cloned. IClonable is often implemented to support
deep-copying of an object. In contrast to a shallow copy, a deep copy of an
object duplicates everything directly or indirectly referenced by the fields
in the object.
You should use Object.MemberwiseClone when you want a copy of an object with
the same references as the original and IClonable.Clone when you want a copy
of an entire object graph.
Anders Nor?s
http://dotnetjunkies.com/weblog/anoras/