C#中Array类实现的接口是什么?
System.Array实现接口,例如ICloneable,IList,ICollection和IEnumerable等。ICloneable接口创建现有对象的副本,即克隆。
让我们了解一下ICloneable接口。它只有一个Clone()方法,因为它会创建一个新对象,该对象是当前实例的副本。
以下是显示如何使用ICloneable接口执行克隆的示例-
示例
using System;
class Car : ICloneable {
int width;
public Car(int width) {
this.width = width;
}
public object Clone() {
return new Car(this.width);
}
public override string ToString() {
return string.Format("Width of car = {0}",this.width);
}
}
class Program {
static void Main() {
Car carOne = new Car(1695);
Car carTwo = carOne.Clone() as Car;
Console.WriteLine("{0}mm", carOne);
Console.WriteLine("{0}mm", carTwo);
}
}现在让我们看看如何在C#中使用Array.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();
}
}热门推荐
10 祝女儿简短祝福语大全
11 大学新年祝福语简短创意
12 元旦适合的祝福语简短
13 朋友出远门祝福语简短
14 初六简短的祝福语
15 祝男孩生日祝福语简短
16 同事调离的祝福语简短
17 拜年红包的祝福语简短
18 妈妈生日祝福语简短励志