Create a copy of an object in C#
This post will discuss how to copy objects in C#.
Copying an object is creating a copy of an existing object. This is usually done to modify or move the copy without impacting the original object.
1. Using Object.MemberwiseClone() method
The Object.MemberwiseClone() method can be used to create a shallow copy of the current Object. Refer to the Microsoft documentation to implement a deep copy with the MemberwiseClone() method.
The following example performs a shallow copy operation on X’s object using the MemberwiseClone() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
using System; public class X { public string str; public object Clone() { return this.MemberwiseClone(); } } public class Example { public static void Main() { X obj = new X(); obj.str = "Hello!"; X copy = (X) obj.Clone(); Console.WriteLine(copy.str); } } /* Output: Hello! */ |
2. Copy Constructor
The Copy Constructor takes another instance of the same class and defines the compiler’s actions when copying the object. The copy constructor implementation should perform deep copy for any referenced objects in the class by creating new objects and copying the immutable type’s values.
The following code example shows how to implement the copy constructor method. It also implements a static copy factory method that essentially does the same thing as the copy constructor method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
using System; public class X { public string str; public X() {} // Copy constructor public X(X other) { this.str = other.str; } // Copy factory public static X GetInstance(X x) { return new X(x); } } public class Example { public static void Main() { X obj = new X(); obj.str = "Hello!"; X copy1 = new X(obj); Console.WriteLine(copy1.str); X copy2 = X.GetInstance(obj); Console.WriteLine(copy2.str); } } |
The problem with the copy constructors is their maintenance, i.e., if an object is structurally modified, you have to modify the copy constructor.
3. Deep Clone
The following code example demonstrates how to implement the deep cloning through BinaryFormatter Serialize() and Deserialize() methods. For this to work, mark your class as serializable through [Serializable].
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
using System; using System.IO; using System.Runtime.Serialization.Formatters.Binary; [Serializable] public class X { public string str; } public static class Extensions { public static T DeepClone<T>(this T obj) { using (MemoryStream stream = new MemoryStream()) { BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, obj); stream.Position = 0; return (T) formatter.Deserialize(stream); } } } public class Example { public static void Main() { X obj = new X(); obj.str = "Hello!"; X copy = obj.DeepClone(); Console.WriteLine(copy.str); } } /* Output: Hello! */ |
That’s all about creating a copy of an object in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)