在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));
// cloned array
Console.WriteLine(string.Join(",", arrCloned));
Console.WriteLine();
}
}
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C编程
C++
C#
MongoDB
MySQL
Javascript
PHP