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
參考:
相關用法
- C# MathF.Sin()用法及代碼示例
- C# MathF.Cos()用法及代碼示例
- C# MathF.Abs()用法及代碼示例
- C# MathF.Tan()用法及代碼示例
- C# MathF.Min()用法及代碼示例
- C# MathF.Max()用法及代碼示例
- C# MathF.Log()用法及代碼示例
- C# MathF.Pow()用法及代碼示例
- C# MathF.Exp()用法及代碼示例
- C# UInt64.Equals用法及代碼示例
- C# Single.IsNaN()用法及代碼示例
- C# UInt32.Equals用法及代碼示例
- C# UInt16.Equals用法及代碼示例
- C# UInt16.CompareTo()用法及代碼示例
- C# UInt64.CompareTo()用法及代碼示例
注:本文由純淨天空篩選整理自SoumikMondal大神的英文原創作品 Object.MemberwiseClone Method in C# with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。