November 2002
Intermediate to advanced
432 pages
9h 20m
English
Assigning one reference variable to another simply creates a second reference to the same object. To make a second copy of an object, one needs some mechanism to create a new instance of the same class and initialize it based on the state of the original object. The Object.MemberwiseClone method does exactly that; however, it is not a public method. Rather, objects that wish to support cloning typically implement the System.ICloneable interface, which has one method, Clone:
namespace System {
public interface ICloneable {
Object Clone();
}
}
The MemberwiseClone method performs what is called a shallow copy, which means that it simply copies each field's value from the source object to the clone. If the field is an object reference, ...
Read now
Unlock full access