当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C# Object.MemberwiseClone用法及代码示例


Object.MemberwiseClone方法用于创建当前对象的浅拷贝或进行克隆。浅拷贝是对象的按位拷贝。在这种情况下,将创建一个新对象,并且该对象具有现有对象的精确副本。本质上,此方法将当前对象的非静态字段复制到新对象。

  • 如果字段是引用类型,则仅复制引用,而不复制引用的对象。因此,在这里,克隆的对象和原始对象将引用同一对象。
  • 如果该字段是值类型,则将执行该字段的逐位复制。

用法:

protected object MemberwiseClone ();

返回值:此方法返回一个Object,它是现有Object的浅拷贝。


范例1:

// C# program to clone a object 
// using MemberwiseClone() method 
using System; 
  
class GFG1 { 
  
    public int val; 
  
    public GFG1(int val) 
    { 
        this.val = val; 
    } 
      
} 
  
class GFG2 { 
  
    public GFG1 gg; 
  
    public GFG2(int val) 
    { 
        // copy the reference of GFG1 to gg 
        this.gg = new GFG1(val);    
    } 
  
    // method for cloning 
    public GFG2 Clone() 
    { 
        // return cloned value using 
        // MemberwiseClone() method 
        return ((GFG2)MemberwiseClone()); 
         
    } 
} 
  
// Driver Code 
class Geek { 
  
  
    // Main Method 
    public static void Main() 
    { 
        // object of Class GFG2 with a value 3 
        GFG2 g = new GFG2(3); 
          
        // calling Clone() 
        // "cc" has the reference of Clone() 
        GFG2 cc = g.Clone(); 
         
        // accessing the main value 
        Console.WriteLine("Value: " + g.gg.val); 
          
        // accessing the cloned value 
        Console.WriteLine("cloned value: " + cc.gg.val); 
          
        // set a new value  
        // in variable "val" 
        cc.gg.val = 6; 
          
        // accessing the main value 
        Console.WriteLine("\nValue: " + g.gg.val); 
         
        // accessing the cloned value 
        Console.WriteLine("cloned value: " + cc.gg.val); 
          
    } 
}
输出:
Value: 3
cloned value: 3

Value: 6
cloned value: 6

范例2:

// C# program to demonstrate the  
// MemberwiseClone() method 
using System; 
  
public class GFG : ICloneable { 
  
    // data members 
    public string Name; 
    public string Surname; 
    public int Age; 
  
    // constructor 
    public GFG(string name,  
     string title, int age) 
    { 
        Name = name; 
        Surname = title; 
        Age = age; 
    } 
  
    // method for cloning 
    public object Clone() 
    { 
        // return cloned value using 
        // MemberwiseClone() method 
        return MemberwiseClone();    
    } 
  
    public override string ToString() 
    { 
        return string.Format("Name = {0}, Surname = {1}, Age {2}", 
                                              Name, Surname, Age); 
    } 
} 
  
// Driver Class 
public class MainClass { 
  
    // Main Method 
    public static void Main() 
    { 
        GFG g = new GFG("ABC", "XYZ", 26); 
  
        // calling Clone() 
        // "cg" has reference of Clone() 
        GFG cg = (GFG)g.Clone(); 
          
        Console.WriteLine("For Old values\nOriginal :"); 
        Console.WriteLine(g); 
  
        Console.WriteLine("Cloned :"); 
        Console.WriteLine(cg); 
  
        Console.WriteLine("\nAfter assigning new values"); 
        g.Name = "LMN"; 
        g.Surname = "QRS"; 
        g.Age = 13; 
  
        Console.WriteLine("Original :"); 
        Console.WriteLine(g); 
  
        Console.WriteLine("Cloned :"); 
  
        // prints the old colned value 
        Console.WriteLine(cg); 
          
    } 
}
输出:
For Old values
Original :
Name = ABC, Surname = XYZ, Age 26
Cloned :
Name = ABC, Surname = XYZ, Age 26

After assigning new values
Original :
Name = LMN, Surname = QRS, Age 13
Cloned :
Name = ABC, Surname = XYZ, Age 26

参考:



相关用法


注:本文由纯净天空筛选整理自SoumikMondal大神的英文原创作品 Object.MemberwiseClone Method in C# with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。