public static void BadExampleReferenceEquality() { var john = new CloneablePerson(new[] { "John", "Smith" }, new CloneableAddress("London Rd", 123)); // Reference equality, jane AND john will change! var jane = john; jane.Names[0] = "Jane"; jane.Address.HouseNumber = 456; Console.WriteLine(john); Console.WriteLine(jane); }
public static void BadExampleICloneable() { // ICloneable is often not a great solution because it is often unclear if it is a shallow clone or deep clone // ICloneable also returns object which can be annoying to constantly cast var john = new CloneablePerson(new[] { "John", "Smith" }, new CloneableAddress("London Rd", 123)); var jane = (CloneablePerson)john.Clone(); jane.Names[0] = "Jane"; jane.Address.HouseNumber = 456; Console.WriteLine(john); Console.WriteLine(jane); }