Clone() method in C#


The Clone() method in C# is used to create a similar copy of the array.

Let us see an example to clone an array using the Clone() method −

Example

 Live Demo

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

Output

one,two,three,four,five
one,two,three,four,five

Above, we have a string array −

string[] arr = { "one", "two", "three", "four", "five" };

With that, in a new string array, we have used the Clone() method with the as operator to clone the array −

string[] arrCloned = arr.Clone() as string[];

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 20-Jun-2020

530 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements