Object.MemberwiseClone Method in C# with Examples Last Updated : 23 Mar, 2022 Comments Improve Suggest changes Like Article Like Report Object.MemberwiseClone Method is used to create a shallow copy or make clone of the current Object. Shallow copy is a bit-wise copy of an object. In this case, a new object is created and that object has an exact copy of the existing object. Basically, this method copies the non-static fields of the current object to the new object. If a field is a reference type, then the only reference is copied not the referred object. So here, the cloned object and the original object will refer to the same object.If the field is value type then the bit-by-bit copy of the field will be performed. Syntax: protected object MemberwiseClone (); Returns: This method returns a Object, which is the shallow copy of existing Object. Example 1: csharp // C# program to clone a object // using MemberwiseClone() method using System; class GFG1 { public int val; public GFG1(int val) { this.val = val; } } class GFG2 { public GFG1 gg; public GFG2(int val) { // copy the reference of GFG1 to gg this.gg = new GFG1(val); } // method for cloning public GFG2 Clone() { // return cloned value using // MemberwiseClone() method return ((GFG2)MemberwiseClone()); } } // Driver Code class Geek { // Main Method public static void Main() { // object of Class GFG2 with a value 3 GFG2 g = new GFG2(3); // calling Clone() // "cc" has the reference of Clone() GFG2 cc = g.Clone(); // accessing the main value Console.WriteLine("Value: " + g.gg.val); // accessing the cloned value Console.WriteLine("cloned value: " + cc.gg.val); // set a new value // in variable "val" cc.gg.val = 6; // accessing the main value Console.WriteLine("\nValue: " + g.gg.val); // accessing the cloned value Console.WriteLine("cloned value: " + cc.gg.val); } } Output:Value: 3 cloned value: 3 Value: 6 cloned value: 6 Example 2: csharp // C# program to demonstrate the // MemberwiseClone() method using System; public class GFG : ICloneable { // data members public string Name; public string Surname; public int Age; // constructor public GFG(string name, string title, int age) { Name = name; Surname = title; Age = age; } // method for cloning public object Clone() { // return cloned value using // MemberwiseClone() method return MemberwiseClone(); } public override string ToString() { return string.Format("Name = {0}, Surname = {1}, Age {2}", Name, Surname, Age); } } // Driver Class public class MainClass { // Main Method public static void Main() { GFG g = new GFG("ABC", "XYZ", 26); // calling Clone() // "cg" has reference of Clone() GFG cg = (GFG)g.Clone(); Console.WriteLine("For Old values\nOriginal :"); Console.WriteLine(g); Console.WriteLine("Cloned :"); Console.WriteLine(cg); Console.WriteLine("\nAfter assigning new values"); g.Name = "LMN"; g.Surname = "QRS"; g.Age = 13; Console.WriteLine("Original :"); Console.WriteLine(g); Console.WriteLine("Cloned :"); // prints the old cloned value Console.WriteLine(cg); } } Output:For Old values Original : Name = ABC, Surname = XYZ, Age 26 Cloned : Name = ABC, Surname = XYZ, Age 26 After assigning new values Original : Name = LMN, Surname = QRS, Age 13 Cloned : Name = ABC, Surname = XYZ, Age 26 Reference: https://docs.microsoft.com/en-us/dotnet/api/system.object.memberwiseclone?view=netframework-4.8 Comment More infoAdvertise with us Next Article Object.MemberwiseClone Method in C# with Examples S SoumikMondal Follow Improve Article Tags : C# CSharp-method CSharp Object Class Similar Reads Type.FindMembers() Method in C# with Examples Type.FindMembers(MemberTypes, BindingFlags, MemberFilter, Object) Method is used to return a filtered array of MemberInfo objects of the specified member type.Syntax: public virtual System.Reflection.MemberInfo[] FindMembers (System.Reflection.MemberTypes memberType, System.Reflection.BindingFlags b 3 min read List.FindIndex() Method in C# with Examples List<T>.FindIndex Method is used to search for an element that matches the conditions defined by a specified predicate and returns the index of the first occurrence within the List<T>. If an item that matches the conditions is not found then this method will return -1. There are 3 method 6 min read Single.Equals() Method in C# with Examples Single.Equals() Method is used to get a value which indicates whether the two instances of Single represents the same value or not. There are 2 methods in the overload list of this method as follows: Equals(Single) Method Equals(Object) Method Single.Equals(Single) Method This method is used to retu 3 min read Double.Equals() Method in C# with Examples Double.Equals() Method is used to get a value that indicates whether the two instances of Double represent the same value or not. There are total of two methods in the overload list of this method as follows: Equals(Double) MethodEquals(Object) MethodDouble.Equals(Double) This method is used to retu 3 min read C# | Boolean.CompareTo(Object) Method Boolean.CompareTo(Object) Method is used to compare the current instance to a specified object and returns an integer which shows their relationship to one another. Syntax: public int CompareTo (object obj); Here, it takes an object to compare to current instance or null. Return Value: This method r 2 min read Like