Beispiel #1
0
        private static void ICloneableVersionCopy()
        {
            // Let us see how we can perform the deep copy now
            MJFinal player = new MJFinal();

            player.Health           = 1;
            player.Felony           = 10;
            player.Money            = 2.0;
            player.Details.Fitness  = 5;
            player.Details.Charisma = 5;

            // lets clone the object but this time perform a deep copy
            MJFinal clonedPlayer = player.Clone() as MJFinal;

            clonedPlayer.Details.Charisma = 10;
            clonedPlayer.Details.Fitness  = 10;

            // Lets see what happened to the original object
            Console.WriteLine("\nOriginal Object:");
            Console.WriteLine("Charisma: {0}, Fitness: {1}",
                              player.Details.Charisma.ToString(),
                              player.Details.Fitness.ToString());
            Console.WriteLine("\nICloneable Deep Cloned Object:");
            Console.WriteLine("Charisma: {0}, Fitness: {1}",
                              clonedPlayer.Details.Charisma.ToString(),
                              clonedPlayer.Details.Fitness.ToString());
        }
        private object DeepCopy()
        {
            MJFinal cloned = this.MemberwiseClone() as MJFinal;

            cloned.Details          = new AdditionalDetails();
            cloned.Details.Charisma = this.Details.Charisma;
            cloned.Details.Fitness  = this.Details.Fitness;

            return(cloned);
        }
        private static void ICloneableVersionCopy()
        {
            // Let us see how we can perform the deep copy now
            MJFinal player = new MJFinal();
            player.Health = 1;
            player.Felony = 10;
            player.Money = 2.0;
            player.Details.Fitness = 5;
            player.Details.Charisma = 5;

            // lets clone the object but this time perform a deep copy
            MJFinal clonedPlayer = player.Clone() as MJFinal;
            clonedPlayer.Details.Charisma = 10;
            clonedPlayer.Details.Fitness = 10;

            // Lets see what happened to the original object
            Console.WriteLine("\nOriginal Object:");
            Console.WriteLine("Charisma: {0}, Fitness: {1}",
                player.Details.Charisma.ToString(),
                player.Details.Fitness.ToString());
            Console.WriteLine("\nICloneable Deep Cloned Object:");
            Console.WriteLine("Charisma: {0}, Fitness: {1}",
                clonedPlayer.Details.Charisma.ToString(),
                clonedPlayer.Details.Fitness.ToString());
        }