C# 中的 Clone() 方法
csharpprogrammingserver side programming更新于 2025/9/23 7:30:00
C# 中的 Clone() 方法用于创建数组的类似副本。
让我们看一个使用 Clone() 方法克隆数组的示例 −
示例
using System;
class Program {
static void Main() {
string[] arr = { "one", "two", "three", "four", "five" };
string[] arrCloned = arr.Clone() as string[];
Console.WriteLine(string.Join(",", arr));
// 克隆数组
Console.WriteLine(string.Join(",", arrCloned));
Console.WriteLine();
}
}
输出
one,two,three,four,five one,two,three,four,five
上面,我们有一个字符串数组 −
string[] arr = { "one", "two", "three", "four", "five" };
这样,在一个新的字符串数组中,我们使用了 Clone() 方法和 as 运算符来克隆数组 −
string[] arrCloned = arr.Clone() as string[];

