首頁  >  文章  >  後端開發  >  C# 中的克隆()

C# 中的克隆()

WBOY
WBOY原創
2024-09-03 15:15:27705瀏覽

C# 中的 Clone() 是一個字串方法,用於傳回物件的精確副本。它傳回字串的實例。傳回的只是具有不同視圖的副本。如果我們想克隆一個數組,這個方法也很有用。對於數組,它會建立具有相同元素數量的數組的副本。 ICloneable 介面中的 Clone 方法允許複製資料。您不需要為此方法提供任何參數。

實作clone()的語法

public object Clone()

實作 ICloneable() 的語法

public interface ICloneable
{
object Clone();
}

如您所見,它不需要任何參數並傳回一個參考。

如果我們想修改克隆的對象,我們可以,並且這樣做不會修改原始對象。

使用clone()方法可以讓開發者變得更容易,因為他們需要編寫更少的程式碼並且易於理解。除此之外不需要其他特殊屬性;它複製所有屬性。我們只能在類別內部呼叫這個方法。它會傳回一個對象,所以我們在使用這個方法時需要進行強制轉換。最好在所有要複製的類別中實作此方法。我們可以透過兩種技術來實現:1.深複製2.淺複製。

淺複製是建立一個新對象,然後將目前對象的非靜態欄位複製到新對象。另一方面,深度複製是建立一個新對象,然後將目前對象的非靜態欄位複製到新對象。

C# 中的clone() 範例

下面的範例展示如何在C#中實作clone()和ICloneable介面。

範例#1

代碼

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Clone
{
class Program
{
static void Main(string[] args)
{
string s1 = "This is C# programming";
string s2 = (String)s1.Clone();
// Displaying both the strings
Console.WriteLine("String to be cloned : {0}", s1);
Console.WriteLine("Cloned String : {0}", s2);
Console.ReadLine();
}
}
}

在上面的例子中,我們需要克隆一個字串。 Clone() 用來克隆這個字串物件。它傳回資料的另一個副本。所以我們可以說回傳值與另一個視圖是相同的資料。此方法不需要任何參數。在輸出中,您可以看到它顯示了原始字串和克隆字串,這是原始字串的精確副本。

輸出

C# 中的克隆()

範例#2

代碼

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Clone
{
class Program
{
static void Main(string[] args)
{
// array initialization
string[] arraytobecloned = { "This", "is", "C#", "clone", "example"};
string[] clonedarray = arraytobecloned.Clone() as string[];   //cloning array
// display original and cloned arrays
Console.WriteLine(string.Join(",", arraytobecloned));
Console.WriteLine(string.Join(",", clonedarray));
Console.WriteLine();
Console.ReadLine();
}
}
}

在上面的範例中,定義了一個包含一組元素的數組,需要對其進行複製。 Clone() 用於克隆數組的所有元素。在輸出中,您可以看到它建立了一個類似的陣列副本。

輸出

C# 中的克隆()

範例#3

代碼

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Clone
{
class Program
{
static void Main(string[] args)
{
string[] arraytobecloned = { "This", "is", "C#", "clone", "example" };
string[] clonedarray = arraytobecloned.Clone() as string[];
Console.WriteLine(string.Join(",", arraytobecloned));
Console.WriteLine(string.Join(",", clonedarray));
Console.WriteLine();
clonedarray[4] = "demo";   // providing new value to cloned array element
Console.WriteLine(string.Join(",", arraytobecloned));       // displaying arrays
Console.WriteLine(string.Join(",", clonedarray));
Console.ReadLine();
}
}
}

在上面的範例中,定義了一個陣列集合,包含不同的元素。 clone() 方法用於克隆這些元素。我們也可以更改該克隆數組的任何元素的值。輸出首先顯示給定數組和克隆數組。我們也可以透過傳遞其索引位置來更改該值。傳遞值後,它會顯示帶有一組新值的克隆數組。所以這意味著我們可以修改克隆數組的值,而不會影響原始數組元素的值。

輸出

C# 中的克隆()

範例#4

代碼

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Clone
{
class Employee : ICloneable    // implementing ICloneable interface
{
int empcode;
string name;
public Employee(int empcode, string name)
{
this.empcode = empcode;
this.name = name;
}
public object Clone()
{
return new Employee(this.empcode, this.name);
}
public override string ToString()
{
return string.Format("empcode = {0}, name = {1},",
this.empcode,
this.name
);
}
}
class Program
{
static void Main()      // main method
{
Employee e1 = new Employee(10, "John");
Employee e2 = e1.Clone() as Employee;
Console.WriteLine("1. {0}", e1);
Console.WriteLine("2. {0}", e2);
Console.ReadLine();
}
}
}

在上面的範例中,我們使用ICloneable介面和clone()方法來複製物件。使用一組參數呼叫公共建構函式後,您應該呼叫克隆方法。

輸出

C# 中的克隆()

範例#5

代碼

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Clone
{
class Program
{
static void Main(string[] args)
{
//declare and initialize a stack
Stack stack1 = new Stack();
stack1.Push(10);
stack1.Push(20);
stack1.Push(30);
stack1.Push(40);
stack1.Push(50);
Console.WriteLine("Stack elements are...");
foreach (int val in stack1)
{
Console.WriteLine(val);
}
Stack stack2 = (Stack)stack1.Clone();
Console.WriteLine("Stack cloned elements are");
foreach (int val in stack2)
{
Console.WriteLine(val);
Console.ReadLine();
}
}
}
}

上面的例子定義了一個堆疊,使用push方法插入元素。堆。 clone() 方法克隆堆疊及其所有元素。它使用 foreach 顯示原始堆疊和克隆堆疊以及所有元素。

輸出

C# 中的克隆()

結論

clone() 函數複製一個物件並傳回實例。使用這種方法,我們可以克隆一個具有相同數量元素的陣列。 「ICloneable」的實作還包括呼叫用於資料複製的克隆方法。實作複製並不是一件好事,因為它會讓開發人員更容易編寫程式碼。

以上是C# 中的克隆()的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:C# 中的修剪()下一篇:C# 中的修剪()