Hi Guys
NP95 shallow copy/deep copy
If
p3: X: 100 Y: 100
p4: X: 100 Y: 100
I mean this is shallow copy
p4: X: 0 Y: 100
I mean this is deep copy
That means if p3 is independent of p4 it is a deep copy
In this program MemberwiseClone() is producing deep copy.
I wish to know whether my understanding is correct.
Please explain.
Thank you
using System;
// The Point class supports deep copy semantics a la ICloneable.
public class Point : ICloneable
{
public int x, y;
public Point(int x, int y)
this.x = x;
this.y = y;
}
// Return a copy of the current object.
public object Clone()
// Copy each field of the Point member by member.
return this.MemberwiseClone();
public override string ToString()
return "X: " + x + " Y: " + y;
public class CloneDemo
public static void Main()
// Notice Clone() returns a generic object type.
// You must perform an explicit cast to obtain the derived type.
Point p3 = new Point(100, 100);
Point p4 = (Point)p3.Clone();
// Change p4.x (which will not change p3.x).
p4.x = 0;
// Print each object.
Console.WriteLine("p3: {0}", p3);
Console.WriteLine("p4: {0}", p4);
/*
*/