asp net 3.5unleashed

Page 148

Object Cloning At times, you want to take an existing object and make a copy of it. For example, you might want to make a copy of an Int32, a String, an ArrayList, a Delegate, or some other object. For some types, however, cloning an object instance doesn’t make sense. For example, it doesn’t make sense to clone a System.Threading.Thread object since creating another Thread object and copying its fields doesn’t create a new thread. Also, for some types, when an instance is constructed, the object is added to a linked list or some other data structure. Simple object cloning would corrupt the semantics of the type. A class must decide whether or not it allows instances of itself to be cloned. If a class wants instances of itself to be cloneable, the class should implement the ICloneable interface, which is defined as follows. (I’ll talk about interfaces in depth in Chapter 15.)

public interface ICloneable { Object Clone(); } This interface defines just one method, Clone. Your implementation of Clone is supposed to construct a new instance of the type and initialize the new object’s state so that it is identical to the original object. The ICloneable interface doesn’t explicitly state whether Clone should make a shallow copy of its fields or a deep copy. So you must decide for yourself what makes the most sense for your type and then clearly document what your Clone implementation does. Note For those of you who are unfamiliar with the term, a shallow copy is when the values in an object’s fields are copied but what the fields refer to is not copied. For example, if an object has a field that refers to a string and you make a shallow copy of the object, then you have two objects that refer to the same string. On the other hand, a deep copy is when you make a copy of what an object’s fields refer to. So if you made a deep copy of an object that has a field that refers to a string, you’d be creating a new object and a new string—the new object would refer to the new string. The important thing to note about a deep copy is that the original and the new object share nothing; modifying one object has no effect on the other object. Many developers implement Clone so that it makes a shallow copy. If you want a shallow copy made for your type, implement your type’s Clone method by calling System.Object’s protected MemberwiseClone method, as demonstrated here:

class MyType : ICloneable { public Object Clone() { return MemberwiseClone(); } } Internally, Object’s MemberwiseClone method allocates memory for a new object. The new object’s type matches the type of the object referred to by the this reference. MemberwiseClone then iterates through all the instance fields for the type (and its base types) and copies the bits from the original object to the new object. Note that no constructor is called for the new object—its state will simply match that of the original object. Alternatively, you can implement the Clone method entirely yourself, and you don’t have to call Object’s MemberwiseClone method. Here’s an example:

class MyType : ICloneable {


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.