private static void PerformDeepCopy() { // Let us see how we can perform the deep copy now MJExtended playerEx2 = new MJExtended(); playerEx2.Health = 1; playerEx2.Felony = 10; playerEx2.Money = 2.0; playerEx2.Details.Fitness = 5; playerEx2.Details.Charisma = 5; // lets clone the object but this time perform a deep copy MJExtended shallowClonedPlayer2 = playerEx2.Clone() as MJExtended; shallowClonedPlayer2.Details.Charisma = 10; shallowClonedPlayer2.Details.Fitness = 10; // Lets see what happened to the original object Console.WriteLine("\nOriginal Object:"); Console.WriteLine("Charisma: {0}, Fitness: {1}", playerEx2.Details.Charisma.ToString(), playerEx2.Details.Fitness.ToString()); Console.WriteLine("\nDeep Cloned Object:"); Console.WriteLine("Charisma: {0}, Fitness: {1}", shallowClonedPlayer2.Details.Charisma.ToString(), shallowClonedPlayer2.Details.Fitness.ToString()); }
private static void PerformShallowCopy() { // The code to demonstrate the shallow copy MJExtended playerEx = new MJExtended(); playerEx.Health = 1; playerEx.Felony = 10; playerEx.Money = 2.0; playerEx.Details.Fitness = 5; playerEx.Details.Charisma = 5; // Lets clone the above object and change the // proprties of contained object MJExtended shallowClonedPlayer = playerEx.Clone() as MJExtended; shallowClonedPlayer.Details.Charisma = 10; shallowClonedPlayer.Details.Fitness = 10; // Lets see what happened to the original object Console.WriteLine("\nOriginal Object:"); Console.WriteLine("Charisma: {0}, Fitness: {1}", playerEx.Details.Charisma.ToString(), playerEx.Details.Fitness.ToString()); Console.WriteLine("\nShallow Cloned Object:"); Console.WriteLine("Charisma: {0}, Fitness: {1}", shallowClonedPlayer.Details.Charisma.ToString(), shallowClonedPlayer.Details.Fitness.ToString()); }