C# tips and tricks 22 – What is object cloning? What is shallow copy? What is deep copy?

4
1



What is Object Cloning?
Like our facebook page

What is shallow copy and deep copy of the object?
shallow copy vs deep copy
Differences between shallow copy and deep copy

Object Cloning

Cloning is a process of creating a new object from an existing object.

We can achieve cloning using two techniques
Shallow copy
Deep copy

Using clone, you can create a new object of the same type as the one you invoke it on, without knowing the exact type of the object you are invoking it on.
Cloning is required when we assign an object to another object, we are assigning two objects to the same reference.

Shallow Copying
Shallow copying is creating a new object and then copying the non- static fields of the current object to the new object.
If the field is a value type, a bit by bit copy of the field is performed.
If the field is a reference type, the reference is copied but the referred object is not, therefore the original object and its clone refer to the same object.
A shallow copy of an object is a new object whose instance variables are identical to the old object.
Shallow copy is done by the object method MemberwiseClone().

Deep Copying
Deep copy is creating a new object and then copying the non-static fields of the current object to the new object.
If a field is a value type, a bit by bit copy of the field is performed.
If a field is a reference type, a new copy of the referred object is performed.
A deep copy of an object is a new object with entirely new instance variables, it does not share objects with the old object.
While performing Deep Copy the classes to be cloned must be flagged as [Serializable].

Code for shallow copy and deep copy

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ObjectCloningUsingShallowAndDeepCopy
{
class Program
{
static void Main(string[] args)
{
Student student1 = new Student();
Console.WriteLine(student1.Age);
Console.WriteLine(student1.Teacher.Salary);

Student student2 = student1.ShallowCopy();
Console.WriteLine(student2.Age);
Console.WriteLine(student2.Teacher.Salary);

student2.Age = 30;
Console.WriteLine(student2.Age);
Console.WriteLine(student2.Teacher.Salary);

student2.Teacher.Salary = 5000;
Console.WriteLine(student2.Age);
Console.WriteLine(student1.Teacher.Salary);
Console.WriteLine(student2.Teacher.Salary);

Student student3 = student1.DeepCopy();
student3.Age = 40;
Console.WriteLine(student1.Age);
Console.WriteLine(student3.Age);

student3.Teacher.Salary = 25000;
Console.WriteLine(student1.Teacher.Salary);
Console.WriteLine(student3.Teacher.Salary);
}
}
class Student
{
public Student()
{
Age = 20;
Teacher = new Teacher();
}
public int Age { get; set; }
public Teacher Teacher { get; set; }

public Student ShallowCopy()
{
return (Student)this.MemberwiseClone();
}
public Student DeepCopy()
{
Student student = new Student();
student = (Student)this.MemberwiseClone();
student.Teacher = new Teacher { Salary = this.Teacher.Salary };
return student;
}
}
class Teacher
{
public Teacher()
{
Salary = 10000;
}
public int Salary { get; set; }
}
}

C# tips and tricks series

Nguồn:https://wijstaanvooronzegrondrechten.org/

Xem Thêm Bài Viết Khác:https://wijstaanvooronzegrondrechten.org/cong-nghe

4 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here