Javatpoint Logo
Javatpoint Logo

Object.MemberwiseClone() in C#

The Object.MemberwiseClone() method in the C# language is used to create a shallow copy of an object in the class. Shallow copy means that the values of the fields in the current Object are copied to a new object. It is mainly useful in duplicating an object state. Instead of copying each value individually from an object to a new object, we can use this method to give the same result.

MemberwiseClose() performs a shallow copy. Only the references are copied if the fields in the Object are references, but the objects they point to are. It means the changes to the referenced objects will be reflected in both the original and cloned instances. If the fields in the Object are values, the values are directly copied into the cloned instance, but the changes made to values in the original values will not be reflected in the clone instance.

It is common to use MemberwiseClone() in conjunction with the ICloneable interface; it's important to note the default implementation of ICloneable.Clone() also uses MemberwiseClone() and performs a shallow copy.

Syntax:

It has the following syntax:

This method does not take any parameters. This method returns a reference to a new object. The return type of Object because MemberwiseClone() will create a shallow copy of the Object.

MemberwiseClone() can be faster than other methods of object duplication because it doesn't invoke a constructor. The shallow copy behaviour might lead to unexpected behaviour if changes to the referenced objects are not intended to affect both the original and cloned instances. If a deep copy of the objects includes the referenced Object, some additional logic and custom implementations are necessary. It only copies non-static fields. It does not copy non-public members. A custom cloning method is used if a class has non-public members that need to be cloned.

Example:

Let us take a simple C# program to illustrate the Object.MemberwiseClone().

Output:

Object.MemberwiseClone() in C#

Explanation:

This program uses the MemberwiseClone method to clone the method. This program contains variables like "Name", which represents the Name of the person; "Age", which represents the age of the person; "originalPerson", this instance represents the original person; and "copiedPerson", which represents the shallow copy of the original person.

The program contains methods like Clone(), which implements the ICloneable interface, which uses the MemberwiseClone() method to create a shallow copy of the current "person" Object and return it as an object.

Control flow of the program:

Firstly, creating the person class object and naming it originalPerson with the name "Ramu" and age 30. The Clone method is called on originalPerson. After that, MemberwiseClone() creates a shallow copy and stores this Object is stored in the copiedPerson variable. Finally, the program prints the details of both the original copied person.

Example:

Let us take a C# program to handle Reference Types.

Output:

Object.MemberwiseClone() in C#

Explanation:

This program will create a deep copy of the Object. It copies both the references and the values. The variables in the program are DepartmentName to represent the Name of the department, Name to represent the Name of an employee; Department is reference type to represent the department to which an employee belongs, originalEmployee to represent an instance of Employee class, copiedEmployee is the Object to represents the copy of the original Object.

The Employee class implements the ICloneable interface, providing a method (Clone) for creating a copy of the Object. The implementation relies on MemberwiseClone() to perform the actual cloning. The program prints the details of the original and copied employees, showcasing that the copied employee has the same state as the original employee. It also demonstrates that changes to the referenced department in the copied employee affect both instances.

Conclusion:

In conclusion, Object.MemberwiseClone() is a versatile method that provides a convenient way to create shallow copies of objects, offering a balance between simplicity and performance. Understanding its characteristics and use cases is essential for making informed decisions when incorporating it into C# code.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA




news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news