static void Main(string[] args) { var yash = new Person1(new[] { "Yash", "Dhanwantri" }, new Address1("House123", "Minnesota")); var suzi = yash.DeepCopy(); suzi.Names = new[] { "Suzi", "Oliver" }; suzi.Address = new Address1("HouseSuzi", "Suzi Town"); Console.WriteLine("Performing Deep copy using BinarySerializer"); Console.WriteLine(yash); Console.WriteLine(suzi); var mike = yash.DeepCopyXMLSerializer(); mike.Names[0] = "Mike"; mike.Names[1] = "Taylor"; mike.Address.HouseNumber = "Dungeon95"; mike.Address.City = "Ghost Town"; Console.WriteLine("Performing Deep copy using XMLSerializer"); Console.WriteLine(yash); Console.WriteLine(mike); }
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(); }