C# सूची को कॉपी या क्लोन करने के लिए, सबसे पहले एक सूची सेट करें।
List < string > myList = new List < string > ();
myList.Add("One");
myList.Add("Two"); अब एक स्ट्रिंग सरणी घोषित करें और कॉपी करने के लिए CopyTo () विधि का उपयोग करें।
string[] arr = new string[10]; myList.CopyTo(arr);
आइए सूची को एक-आयामी सरणी में कॉपी करने के लिए पूरा कोड देखें।
उदाहरण
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);
}
}
}