static void Main(string[] args) { //Here it is creating the refrence of one object to another. //so, test2 also point to same memory location as test1 //and if we change the test2 data, it will reflect to test1. Test test1 = new Test(); test1.Name = "Sanjay"; Test test2 = test1; //Trying to make copy in test2. test2.Name = "Reference Stored in test2"; Console.WriteLine("Reference copied: " + test1.Name); //Here we are using to create copy of one object data to another. TestCopyObject obj1 = new TestCopyObject(); obj1.Name = "Sanjay"; TestCopyObject obj2 = obj1.CloneMe(obj1); //Trying to make copy in obj2. obj2.Name = "Here we are cloning the obj1"; Console.WriteLine("Clone of obj1: " + obj1.Name); Console.ReadLine(); }
public TestCopyObject CloneMe(TestCopyObject test) { return((TestCopyObject)this.MemberwiseClone()); }