Home  >  Article  >  Backend Development  >  What interfaces does the Array class in C# implement?

What interfaces does the Array class in C# implement?

WBOY
WBOYforward
2023-09-16 16:17:041278browse

What interfaces does the Array class in C# implement?

System.Array implements interfaces such as ICloneable, IList, ICollection and IEnumerable. The ICloneable interface creates a copy of an existing object, a clone.

Let us understand the ICloneable interface. It only has a Clone() method because it creates a new object that is a copy of the current instance.

The following example shows how to perform cloning using the ICloneable interface-

Example

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

Now let us see how to clone an array using Array.Clone in C#-

Example

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

The above is the detailed content of What interfaces does the Array class in C# implement?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete