예제 #1
0
        private static void PerformDeepCopy()
        {
            // Let us see how we can perform the deep copy now
            OtherHero playerEx2 = new OtherHero();

            playerEx2.Health           = 1;
            playerEx2.Courage          = 10;
            playerEx2.Money            = 2.0;
            playerEx2.Details.Fitness  = 5;
            playerEx2.Details.Charisma = 5;

            // lets clone the object but this time perform a deep copy
            OtherHero shallowClonedPlayer2 = playerEx2.Clone() as OtherHero;

            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());
        }
예제 #2
0
        private static void PerformShallowCopy()
        {
            // The code to demonstrate the shallow copy
            OtherHero playerEx = new OtherHero();

            playerEx.Health           = 1;
            playerEx.Courage          = 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
            OtherHero shallowClonedPlayer = playerEx.Clone() as OtherHero;

            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());
        }
예제 #3
0
        private AProtagonistEx DeepCopy()
        {
            OtherHero cloned = this.MemberwiseClone() as OtherHero;

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

            return(cloned as AProtagonistEx);
        }