c# 浅拷贝vs深拷贝

浅拷贝(Shallow Copy)

定义

创建一个新对象,然后将当前对象的值类型字段复制到新对象中。 但是当数据是引用类型时,复制唯一的引用而不是引用的对象本身。 因此 original 和 clone 指的是同一个对象。
c# 浅拷贝vs深拷贝_第1张图片

using System;
      
class Example {
      
// Main Method
static void Main(string[] args)
{
      
    Company c1 = new Company(548, "GeeksforGeeks",
                                  "Sandeep Jain");
      
    // Performing Shallow copy                      
    Company c2 = (Company)c1.Shallowcopy(); 
  
    Console.WriteLine("Before Changing: ");
      
    // Before changing the value of
    // c2 GBRank and CompanyName
    Console.WriteLine(c1.GBRank);
    Console.WriteLine(c2.GBRank);
    Console.WriteLine(c2.desc.CompanyName);
    Console.WriteLine(c1.desc.CompanyName);
  
    // changing the value of c2 GBRank
    // and CompanyName
    c2.GBRank = 59;
    c2.desc.CompanyName = "GFG";
  
    Console.WriteLine("\nAfter Changing: ");
      
    // After changing the value of 
    // c2 GBRank and CompanyName
    Console.WriteLine(c1.GBRank);
    Console.WriteLine(c2.GBRank);
    Console.WriteLine(c2.desc.CompanyName);
    Console.WriteLine(c1.desc.CompanyName);
}
}
  
  
class Company 
{
          
    public int GBRank;
    public CompanyDescription desc;
      
    public Company(int gbRank, string c,
                               string o)
    {
        this.GBRank = gbRank;
        desc = new CompanyDescription(c, o);
    }
      
    // method for cloning object
    public object Shallowcopy()
    {
        return this.MemberwiseClone();
    }
      
    // method for cloning object
    public Company DeepCopy()
    {
        Company deepcopyCompany = new Company(this.GBRank,
                            desc.CompanyName, desc.Owner);
        return deepcopyCompany;
    }
}
      
      
class CompanyDescription 
{
      
    public string CompanyName;
    public string Owner;
    public CompanyDescription(string c, string o)
    {
        this.CompanyName = c;
        this.Owner = o;
    }
}

Before Changing:
548
548
GeeksforGeeks
GeeksforGeeks

After Changing:
548
59
GFG
GFG

深拷贝(Deep Copy)

定义

它是一个创建新对象,然后将当前对象的字段复制到新创建的对象中,以完整复制内部引用类型的过程。 如果指定的字段是值类型,则将执行该字段的逐位复制。 如果指定的字段是引用类型,则执行引用对象的新副本。

c# 浅拷贝vs深拷贝_第2张图片

using System;
  
namespace ShallowVSDeepCopy {
      
class Program {
      
    // Main Method
    static void Main(string[] args)
    {
        Company c1 = new Company(548, "GeeksforGeeks",
                                      "Sandeep Jain");
        // Performing Deep copy                             
        Company c2 = (Company)c1.DeepCopy(); 
  
        Console.WriteLine("Before Changing: ");
           
        // Before changing the value of 
        // c2 GBRank and CompanyName
        Console.WriteLine(c1.GBRank);
        Console.WriteLine(c2.GBRank);
        Console.WriteLine(c2.desc.CompanyName);
        Console.WriteLine(c1.desc.CompanyName);
  
        Console.WriteLine("\nAfter Changing: ");
          
        // changing the value of c2 
        // GBRank and CompanyName
        c2.GBRank = 59;
        c2.desc.CompanyName = "GFG";
  
        // After changing the value of
        // c2 GBRank and CompanyName
        Console.WriteLine(c1.GBRank);
        Console.WriteLine(c2.GBRank);
        Console.WriteLine(c2.desc.CompanyName);
        Console.WriteLine(c1.desc.CompanyName);
    }
}
  
class Company {
      
    public int GBRank;
    public CompanyDescription desc;
  
    public Company(int gbRank, string c, 
                               string o)
    {
        this.GBRank = gbRank;
        desc = new CompanyDescription(c, o);
    }
      
    // method for cloning object
    public object Shallowcopy()
    {
        return this.MemberwiseClone();
    }
      
    // method for cloning object
    public Company DeepCopy()
    {
        Company deepcopyCompany = new Company(this.GBRank,
                           desc.CompanyName, desc.Owner);
                             
        return deepcopyCompany;
    }
}
  
class CompanyDescription {
      
    public string CompanyName;
    public string Owner;
    public CompanyDescription(string c, 
                             string o)
    {
        this.CompanyName = c;
        this.Owner = o;
    }
}
}

Before Changing:
548
548
GeeksforGeeks
GeeksforGeeks

After Changing:
548
59
GFG
GeeksforGeeks

你可能感兴趣的:(c#,c#,开发语言)