What interfaces does the Array class in C# implement?

WBOY
Release: 2023-09-16 16:17:04
forward
1284 people have browsed it

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);
   }
}
Copy after login

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

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template