Exemplo n.º 1
0
        protected override Base CloneImpl()
        {
            Derived2T cloned = (Derived2T)base.CloneImpl();

            // base members have been taken care of in the base impl.
            // we only add our own stuff.
            cloned.der2Data = (DeepDataT)der2Data.Clone();
            return(cloned);
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            var obj1 = new Derived2T(1, 2);

            obj1.txt = "this is obj1";

            var obj2 = (Derived2T)obj1.Clone();

            obj2.der1Data.i++;
            obj2.der2Data.i++;                // changes value.
            obj2.txt = "this is a deep copy"; // replaces reference.

            // the values for i should differ because
            // we performed a deep copy of the DeepDataT members.
            Console.WriteLine("obj1 txt, i1, i2: " + obj1.txt + ", " + obj1.der1Data.i + ", " + obj1.der2Data.i);
            Console.WriteLine("obj2 txt, i1, i2: " + obj2.txt + ", " + obj2.der1Data.i + ", " + obj2.der2Data.i);
        }