예제 #1
0
        static void Main(string[] args)
        {
            WriteLine("Straight forward copying:");
            var john = new Person(new[] { "John", "Smith" },
                                  new Address("LondonRoad", 123));

            var jane = john;

            jane.Names[0]            = "Jane";
            jane.Address.HouseNumber = 321;

            WriteLine(john);
            WriteLine(jane);

            #region ICloneable
            WriteLine();
            WriteLine("ICloneable approach:");
            var john1 = new Person1(new[] { "John", "Smith" },
                                    new Address1("LondonRoad", 123));

            var jane1 = (Person1)john1.Clone();
            jane1.Names[0]            = "Jane";
            jane1.Address.HouseNumber = 321;

            WriteLine(john1);
            WriteLine(jane1);
            #endregion ICloneable

            #region Copy constructor
            WriteLine();
            WriteLine("Copy constructor approach:");
            var john2 = new Person2(new [] { "John", "Smith" },
                                    new Address2("LondonRoad", 123));

            var jane2 = new Person2(john2)
            {
                Names   = new[] { "Jane" },
                Address = { HouseNumber = 321 }
            };

            WriteLine(john2);
            WriteLine(jane2);
            #endregion Copy constructor

            #region Deep copy
            WriteLine();
            WriteLine("Deep copy approach:");
            var john3 = new Person3(new[] { "John", "Smith" },
                                    new Address3("LondonRoad", 123));

            var jane3 = john3.DeepCopy();
            jane3.Names[0]            = "Jane";
            jane3.Address.HouseNumber = 321;

            WriteLine(john3);
            WriteLine(jane3);
            #endregion Deep copy

            #region Serialization
            WriteLine();
            WriteLine("Serialization approach:");
            var john4 = new Person4(new[] { "John", "Smith" },
                                    new Address4("LondonRoad", 123));

            var jane4 = john4.DeepCopy();
            jane4.Names[0]            = "Jane";
            jane4.Address.HouseNumber = 321;

            WriteLine(john4);
            WriteLine(jane4);
            #endregion Serialization

            ReadKey();
        }
예제 #2
0
 // CopyConstructor approach
 public Person2(Person2 other)
 {
     Names   = other.Names;
     Address = new Address2(other.Address);
 }