首頁  >  文章  >  後端開發  >  C#中Array類別實作了哪些介面?

C#中Array類別實作了哪些介面?

WBOY
WBOY轉載
2023-09-16 16:17:041249瀏覽

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();
   }
}

以上是C#中Array類別實作了哪些介面?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除