private static void PerformDeepCopy() { // Let us see how we can perform the deep copy now CJEx playerEx2 = new CJEx(); 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 CJEx shallowClonedPlayer2 = playerEx2.Clone() as CJEx; 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 CJEx playerEx = new CJEx(); 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 CJEx shallowClonedPlayer = playerEx.Clone() as CJEx; 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()); }