📜  C#中的浅复制和深复制

📅  最后修改于: 2021-05-29 14:58:58             🧑  作者: Mango

通常,当我们尝试将一个对象复制到另一个对象时,两个对象将共享相同的内存地址。通常,我们使用赋值运算符=来复制引用,而不是对象,除非存在值类型字段。该运算符将始终复制参考,而不是实际对象。例如:假设G1指向内存地址5000,那么G2也将指向5000。因此,如果更改存储在地址5000上的数据值,则G1G2都将显示相同的数据。

Geeks G1 = new Geeks();

// Copy the instance using 
// '=' operator
Geeks G2 = G1;

浅复制:创建一个新对象,然后将当前对象的值类型字段复制到新对象。但是,当数据是引用类型时,则仅复制引用,而不复制被引用的对象本身。因此,原始副本和克隆副本引用相同的对象。当您看到Shallow副本的代码和图表时,该概念将更加清晰。

注意:在上图中,“ Global Rank值类型”字段,因此它将创建该副本的副本并将其存储在其他位置,但“名称(Desc)是“引用类型”字段,因此它指向旧的或主内存位置。

示例:在这里,如果您要更改值类型,则它可以在c2和c1上运行,并且不受影响,但是在引用类型中,任何更改都会影响c1和c2。

// C# program to illustrate the 
// concept of Shallow Copy
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

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

注意:在上图中, The Global Rank值类型”字段,因此它将创建该副本的副本并将其存储在其他位置。 Name( Desc )是引用类型字段,因此在Deep Copy中,存在引用类型字段的副本,该副本也将存储在其他位置。

示例:在此,字段类型并不重要,它是值类型还是引用类型。深层复制会复制整个数据并将其存储在其他内存位置,因此,如果我们更改c2,则c1也会受到影响,反之亦然。

// C# program to demonstrate the
// concept of Deep copy
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