namespace ShallowVsDeepCopyPrj
{
public class Product : ICloneable
{
private String m_ProductName, m_ProductDesc;
private String prodName = "ABC of C#";
private ProductCatalog m_ProductCatalog = new ProductCatalog("ABC of C#", "AVAILABLE");
public Product()
{
this.Setm_ProductDesc = "Test Your C# Skill";
this.Setm_ProductName = prodName;
}
public String Setm_ProductDesc { set { m_ProductDesc = value; } get { return m_ProductDesc; } }
public String Setm_ProductName { set { m_ProductName = value; } get { return m_ProductName; } }
public ProductCatalog Setm_ProductCatalog { set { m_ProductCatalog = value; } get { return m_ProductCatalog; } }
Object ICloneable.Clone() { return this.Clone(); }
public virtual Product Clone()
{
// Start with a flat, Simply Memberwise copy
Product shallowCopy_Prod = this.MemberwiseClone() as Product;
return shallowCopy_Prod;
}
public Product CloneThroughShallowCopy()
{
return this.Clone(); //Return Shallow Copy
}
public virtual Product CloneThroughDeepCopy()
{
// Start with a flat,Creating seperate memory block for reference type members
Product deepCopy_Prod = new Product();
// Then deep-copy everything that needs the - special attention
deepCopy_Prod.Setm_ProductName = this.Setm_ProductName;
deepCopy_Prod.Setm_ProductDesc = this.Setm_ProductDesc;
deepCopy_Prod.Setm_ProductCatalog = this.Setm_ProductCatalog.ClonedThroughDeepCopy();
return deepCopy_Prod;
}
}
public class ProductCatalog : ICloneable
{
private String m_ProductName, m_ProductStatus;
public ProductCatalog()
{
//Do nothing
}
public ProductCatalog(string pName, string pCat)
{
this.Setm_ProductName = pName;
this.Setm_ProductStatus = pCat;
}
public String Setm_ProductStatus { set { m_ProductStatus = value; } get { return m_ProductStatus; } }
public String Setm_ProductName { set { m_ProductName = value; } get { return m_ProductName; } }
Object ICloneable.Clone() { return this.Clone(); }
public virtual ProductCatalog Clone()
{
// Start with new ProductCatalog object
ProductCatalog deepCopy_ProdCat = new ProductCatalog();
// Then deep-copy everything that needs the - special attention
deepCopy_ProdCat.Setm_ProductName = this.Setm_ProductName;
deepCopy_ProdCat.Setm_ProductStatus = this.Setm_ProductStatus;
return deepCopy_ProdCat;
}
public ProductCatalog ClonedThroughDeepCopy()
{
return this.Clone();
}
}
}
static void Main(string[] args)
{
Product m_Product = new Product();
try
{
Product ShallowCopyProd = (Product)m_Product.CloneThroughShallowCopy(); // Shallow Copy of Product
Product DeepCopyProd = (Product)m_Product.CloneThroughDeepCopy();//Deep Copy of Product
//Making some changes to m_Product, let's se where it gets reflected and where it is not reflecting
m_Product.Setm_ProductName = "XYZ";
m_Product.Setm_ProductDesc = "XYZ1";
m_Product.Setm_ProductCatalog.Setm_ProductName = "UNKNOWN";
m_Product.Setm_ProductCatalog.Setm_ProductStatus = "NA";
Console.WriteLine("\n");
Console.WriteLine("Output - Shallow Copy Vs Deep Copy in .NET: ");
Console.WriteLine("Source - Product: " + m_Product.Setm_ProductName + "/" + m_Product.Setm_ProductDesc + "/" + m_Product.Setm_ProductCatalog.Setm_ProductName + "/" + m_Product.Setm_ProductCatalog.Setm_ProductStatus);
Console.WriteLine("Shallow Copy - Product: " + ShallowCopyProd.Setm_ProductName + "/" + ShallowCopyProd.Setm_ProductDesc + "/" + ShallowCopyProd.Setm_ProductCatalog.Setm_ProductName + "/" + ShallowCopyProd.Setm_ProductCatalog.Setm_ProductStatus);
Console.WriteLine("Deep Copy - Product: " + DeepCopyProd.Setm_ProductName + "/" + DeepCopyProd.Setm_ProductDesc + "/" + DeepCopyProd.Setm_ProductCatalog.Setm_ProductName + "/" + DeepCopyProd.Setm_ProductCatalog.Setm_ProductStatus);
Console.ReadLine();
}
catch (Exception ex) {
Console.WriteLine(ex.Message.ToString()); }
}
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)