MemberwiseClone by example

I not ashamed of admitting that I do not know everything about everything J

For example, I didn’t know that well what the method MemberwiseClone() method does.

So, first step was to check the MSDN documentation, found here.

"Object.MemberwiseClone Method"

https://msdn.microsoft.com/en-us/library/system.object.memberwiseclone.aspx

This gives the following explanation;

The MemberwiseClone method creates a shallow copy by creating a new object, and then copying the nonstatic fields of the current object to the new object.

If a field is a value type, a bit-by-bit copy of the field is performed. If a field is a reference type, the reference is copied but the referred object is not; therefore, the original object and its clone refer to the same object.

For example, consider an object called X that references objects A and B. Object B, in turn, references object C. A shallow copy of X creates new object X2 that also references objects A and B.

In contrast, a deep copy of X creates a new object X2 that references the new objects A2 and B2, which are copies of A and B. B2, in turn, references the new object C2, which is a copy C.

Use a class that implements the ICloneable interface to perform a deep or shallow copy of an object.

Which to me is not is that clear and further research didn’t give any good examples, so I created my own in order to see what it does.

And without any attempt to explain what is going on, here it is. Feel free to test as you like.

Hopefully this should be self-explanatory.

    class Manager : Employee

    {

        static void Main(string[] args)

        {

            Manager manager1 = new Manager();

            manager1.Name = "Mary Maryson";

            manager1.Age = 45;

            manager1.Department.Name = "Accounting";

            manager1.Department.City = "Stockholm";

            Manager manager2 = (Manager)manager1.MemberwiseClone();

            Console.WriteLine("Manager 1 -> {0}", manager1);

            Console.WriteLine("Manager 2 -> {0}", manager2);

            // Edit the first Manager, notice that this will update the Second managers

            // Department data as well since the reference is cloned, not the object.

         // For the value types, they are not affected.

            manager1.Name = "Mike Mikeson";

            manager1.Age = 55;

            manager1.Department.City = "London";

            Console.WriteLine();

            Console.WriteLine("Manager 1 -> {0}", manager1);

            Console.WriteLine("Manager 2 -> {0}", manager2);

            // And of course, chaning the Department for the manager2 will change the

            // Department data for manager1 as well.

            manager2.Name = "Paul Paulson";

            manager2.Age = 30;

            manager2.Department.City = "Paris";

            Console.WriteLine();

            Console.WriteLine("Manager 1 -> {0}", manager1);

            Console.WriteLine("Manager 2 -> {0}", manager2);

     }

    }

    class Employee

    {

        public string Name = "<default>";

        public int Age = 0;

        public Department Department = new Department();

        public override string ToString()

        {

            return String.Format("{0}, age: {1}, is in {2}", Name, Age, Department);

        }

    }

    class Department

    {

        public string Name = "<default>";

        public string City = "<default>";

       

        public override string ToString()

        {

            return String.Format("{0} / {1}", Name, City);

        }

    }