public static void Main(string[] args) { var lebevila = new Person(new[] { "Le", "Bevilaqua" }, new Address("Diana Street", 1001)); var refag = lebevila.DeepCopyXml(); refag.Names[0] = "Re"; refag.Address.HouseNumber = 200; WriteLine(lebevila); WriteLine(refag); var l = new Line { Start = new Point { X = 1, Y = 2 }, End = new Point { X = 2, Y = 3 } }; WriteLine(l); var lcopy = l.DeepCopy(); lcopy.Start = new Point { X = 55, Y = 11 }; WriteLine(lcopy); }
static void Main(string[] args) { var john = new Person(new [] { "John", "Smith" }, new Address("London Road", 123)); var jane = john.DeepCopyXml(); jane.Names[0] = "Jane"; jane.Address.HouseNumber = 321; Console.WriteLine(john); Console.WriteLine(jane); }
public static void CopyThroughXmlSerialization() { var john = new Person(new[] { "John", "Smith" }, new Address("London Rd", 123)); var jane = john.DeepCopyXml(); jane.Names[0] = "Jane"; jane.Address.HouseNumber = 456; Console.WriteLine(john); Console.WriteLine(jane); }
static void Main(string[] args) { var john = new Person(new [] { "John", "Smith" }, new Address("Park Ave", 100)); var jane = john.DeepCopyXml(); jane.Names = new[] { "Jane", "Jackson" }; jane.Address.HouseNumber = 221; Console.WriteLine(john); Console.WriteLine(jane); var jack = john; }
static void Main(string[] args) { var john = new Person(new[] { "John", "Smith" }, new Address("London Road", 123)); var jane = john.DeepCopyXml(); jane.Names[0] = "Jane"; jane.Address.HouseNumber = 321; WriteLine(john); WriteLine(jane); var john2 = john.DeepCopy(); john2.Address = new Address("Birmingham Lane", 222); WriteLine(john2); }
static void Main(string[] args) { var John = new Person(new[] { "John", "Smith" }, new Address("London Road", 123)); Console.WriteLine(John); var Jane = new Person(John); Jane.Address.HouseNumber = 321; Console.WriteLine(Jane); /*var Jill = John.DeepCopy();*/ var Jill = John.DeepCopyXml(); Jill.Names[0] = "Jill"; Console.WriteLine(Jill); }
static void Main(string[] args) { var john = new Person(new [] { "John", "Smith" }, new Address("London Road", 123)); // var jane = john; // wrong! we're copying a reference to john // jane.Names[0] = "Jane"; // this will modify the Name in both john and jane // var jane = (Person) john.Clone(); // This won't solve the problem since the implementation of clone does a shallow copy // jane.Address.HouseNumber = 321; // this will change the property in both objects // var jane = new Person(john); // jane.Address.HouseNumber = 321; // everything is working correctly // var jane = john.DeepCopy(); Uncomment the Serializable attribute var jane = john.DeepCopyXml(); jane.Names[0] = "Jane"; jane.Address.HouseNumber = 321; Console.WriteLine(john); Console.WriteLine(jane); Console.ReadLine(); }