Shallow Copy:
Shallow copy is the way copying an object's value type fields bit by bit into target object and object's reference types are copied as references into the target object but not the referenced object itself. This can be done in C# using MemberwiseClone() method on an object. As in MSDN, "The MemberwiseClone method creates a shallow copy by creating a new object, and then copying the nonstatic fields of the current object to the new object."
Example: the following clsShallow class is to be cloned which includes value types (like Age) and ref types (like EmpSalary is a class):
public class clsShallow
{
public static string CompanyName = "My Company";
public int Age;
public string EmployeeName;
public clsRefSalary EmpSalary;
public clsShallow CreateShallowCopy(clsShallow inputcls)
{
return (clsShallow)inputcls.MemberwiseClone();
}
}
public class clsRefSalary
{
public clsRefSalary(int _salary)
{ Salary = _salary; }
public int Salary; }
Now let us debug and trace the outputs to do shallow copy using the CreateShallowCopy() method.
First, use the following code to call the CreateShallowCopy method from other classes.
// Creates an instance of clsShallow and assign values to its fields.
clsShallow objshallow = new clsShallow();
objshallow.Age = 25;
objshallow.EmployeeName = "Ahmed Eid";
// add the ref value to the objshallow
clsRefSalary clsref = new clsRefSalary(1000);
objshallow.EmpSalary = clsref;
// Performs a shallow copy of m1 and assign it to
m2. clsShallow m2 = objshallow.CreateShallowCopy(objshallow);
// then modify the clsref salary value to be 2000 clsref.Salary = 2000;
// so the m1 object salary value become 2000 int EmpSalary = objshallow.EmpSalary.Salary;
After assigning the values (value and ref types) to the object objShallow and before doing the shallow copy, the values are (for the current object value): Age: 25 (value type), EmpSalry: has salary value of 1000 (ref type).
then do the shallow copy and modify the value of clsref.salary, reference field type, then check the value of m2, the newly created object (ref and value fields) again.
The values are (for the newly created object): Age: 25 (value type), a new copy of the objShallow object; EmpSalry: has a salary value of 2000 (ref type), a reference to objShallow.EmpSalry object, which is also referenced to the clsref object.
Note: values of m2.EmpSalry and clsref are the same after modifying the clsref values (reference type concept).
Deep Copy:
Deep copy is the way of copying an object completely bit-by-bit i.e. Deep copy copies value of every field inside an object. For nested objects(objects that contain other objects), deep copy creates new instances for each object inside nested object and copies all the values for fields inside them.
[Serializable] // serialize the classes in case of deep copy public class clsDeep { public static string CompanyName = "My Company"; public int Age; public string EmployeeName; public clsRefSalary EmpSalary; public clsDeep CreateDeepCopy(clsDeep inputcls) { MemoryStream m = new MemoryStream(); BinaryFormatter b = new BinaryFormatter(); b.Serialize(m, inputcls); m.Position = 0; return (clsDeep)b.Deserialize(m); } } [Serializable] public class clsRefSalary { public clsRefSalary(int _salary) { Salary = _salary; } public int Salary; }
Now let us debug and trace the outputs to do deep copy using the CreateDeepCopy() method.
First, use the following code to call the CreateDeepCopy method from other classes.
Collapse | Copy Code
// Creates an instance of clsDeep and assign values to its fields. clsDeep objdeep = new clsDeep(); objdeep.Age = 25; objdeep.EmployeeName = "Ahmed Eid"; // add the ref value clsRefSalary clsref = new clsRefSalary(1000); objdeep.EmpSalary = clsref; // Performs a shallow copy of m1 and assign it to m2. clsDeep m2 = objdeep.CreateDeepCopy(objdeep); // then modify the clsref salary value to be 2000 clsref.Salary = 2000; // so the m1 object salary value become 2000 int EmpSalary = objdeep.EmpSalary.Salary;
No comments:
Post a Comment