// - all derived types must also implement ICloneable // - all of its member types must also support it static void Main(string[] args) { // case 1: clone value type with ref type (string, class and int[]) as member //ErrorMessage errorMessage = new ErrorMessage("msg", new int[] { 1 }, new MyClass { MyProperty = 1 }); //ErrorMessage errorMessageClone = (ErrorMessage)errorMessage.Clone(); //errorMessage.msg = null; errorMessage.val = null; errorMessage.mc = null; //ErrorMessage errorMessage = new ErrorMessage("msg", new int[] { 1 }, new MyClass { MyProperty = 1 }); //ErrorMessage errorMessageCopy = errorMessage; // case 2: refer type with ref type (string, class and int[]) as member // case 3: refer type with ref type (string, class and int[]) as member // WITH derived type //Derived derived = new Derived(); //Derived derived_clone = derived.Clone() as Derived; //if (derived_clone is null) //{ // Console.WriteLine("derived_clone is null"); //} // case 4: don't allow class that implements ICloneable to be inherited Derived_v2 derived_v2 = new Derived_v2(); Derived_v2 derived_v2_clone = derived_v2.Clone() as Derived_v2; if (derived_v2_clone is null) { Console.WriteLine("derived_v2_clone is null"); } }
public object Clone() { Derived_v2 rval = new Derived_v2(this); return(rval); }
private Derived_v2(Derived_v2 right) : base(right) { dValues = right.dValues.Clone() as double[]; }