Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » Memory Management » Downloads
 
Add your own
alternative version
Go to top

Shallow Copy vs. Deep Copy in .NET

, 10 Oct 2008
Shallow and deep copy are used for copying data between objects.
ShallowVsDeep.zip
ShallowVsDeep
bin
Debug
ShallowVsDeep.exe
obj
Debug
TempPE
Properties
using System;
using System.Collections.Generic;
using System.Text;

namespace ShallowVsDeep
{
    class Program
    {
        static void Main(string[] args)
        {
            clsRefSalary clsref; int EmpSalary;
            //
            Console.Write("Shallow Copy Tracing:\n---------------------------------\n");
            // 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
            clsref = new clsRefSalary(1000);
            objshallow.EmpSalary = clsref;

            // Performs a shallow copy of m1 and assign it to m2.
            clsShallow m2 = objshallow.CreateShallowCopy(objshallow);

            clsShallow m3 = m2;

            // then modify the clsref salary value to be 2000 
            clsref.Salary = 2000;

            // so the m1 object salary value become 2000
            EmpSalary = objshallow.EmpSalary.Salary;

            Console.Write("Name: {0}\nAge: {1}\nSalary: {2}\n\n",objshallow.EmployeeName,objshallow.Age,objshallow.EmpSalary.Salary);

            Console.Write("Deep Copy Tracing:\n---------------------------------\n");
            // 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
            clsref = new clsRefSalary(1000);
            objdeep.EmpSalary = clsref;

            // Performs a shallow copy of m1 and assign it to m2.
            clsDeep m4 = objdeep.CreateDeepCopy(objdeep);

            clsDeep m5 = m4;

            // then modify the clsref salary value to be 2000 
            clsref.Salary = 2000;

            // so the m1 object salary value become 2000
            EmpSalary = objdeep.EmpSalary.Salary;

            Console.Write("Name: {0}\nAge: {1}\nSalary: {2}\n\n", objshallow.EmployeeName, objshallow.Age, objshallow.EmpSalary.Salary);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Ahmad Eid Salim
Software Developer (Senior)
Egypt Egypt
No Biography provided

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 11 Oct 2008
Article Copyright 2008 by Ahmad Eid Salim
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid