C# 中的 Clone() 方法
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));
// cloned array
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[];
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP