Interview Question

Interview

What are the different ways to clone an object??

Answer

Interview Answer

1 Answer

1

There's two fundamental ways: shallow copy and deep copy. Shallow copy basically only copies a reference to the original object. It's only used with classes; anything representing a structure or a value type can't be shallow copied. With this copy, you end up with two addresses in memory that contain a pointer to the same object. All properties and fields associated with this object will be shared between the copies; when a value changes via the use of one pointer, the value will simultaneously change for the other pointer. Deep copy means to duplicate every bit of data that represents a class or structure. It produces a value that, when modified, does not modify the original.

Julius on Apr 3, 2010

Add Answers or Comments

To comment on this, Sign In or Sign Up.