How to use the Clone() method of array class in C#?


The Clone() method in C# is used to clone the existing array.

Firstly, set the array to be cloned.

string[] arr = { "Web", "World"};

Now clone the array created above using the array.Clone() method.

string[] arrClone = array.Clone() as string[];

Let us see the complete example.

Example

using System;
class Program {
   static void Main() {
      string[] arr = { "Web", "World"};
      Console.WriteLine(string.Join(",", arr));
      string[] arrClone = array.Clone() as string[];
      Console.WriteLine(string.Join(",", arrClone));
      Console.WriteLine();
   }
}

Updated on: 23-Jun-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements