Home >Backend Development >C#.Net Tutorial >C# program to clone or copy a list
To copy or clone a C# list, first set up a list.
List < string > myList = new List < string > (); myList.Add("One"); myList.Add("Two");
Now declare an array of strings and copy using the CopyTo() method.
string[] arr = new string[10]; myList.CopyTo(arr);
Let’s see the complete code for copying a list to a one-dimensional array.
using System; using System.Collections.Generic; using System.Linq; public class Demo { public static void Main() { List < string > myList = new List < string > (); myList.Add("One"); myList.Add("Two"); myList.Add("Three"); myList.Add("Four"); Console.WriteLine("First list..."); foreach(string value in list1) { Console.WriteLine(value); } string[] arr = new string[20]; myList.CopyTo(arr); Console.WriteLine("After copy..."); foreach(string value in arr) { Console.WriteLine(value); } } }
The above is the detailed content of C# program to clone or copy a list. For more information, please follow other related articles on the PHP Chinese website!