
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(); }}
以上就是C#中Array类实现了哪些接口?的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1435485.html
微信扫一扫
支付宝扫一扫