C# List<T> class provides methods and properties to create a list of objects (classes).
List is a generic class. You must import the following namespace before using the List<T> class.
- using System.Collections.Generic;
The CopyTo method copies a List into a one-dimensional array. The CopyTo method has three overloaded forms.
The following code snippet copies the entire list into an Array.
- using System;
- using System.Collections.Generic;
- namespace ConsoleApp1
- {
- class Program
- {
- static void Main(string[] args)
- {
- List<string> AuthorList = new List<string>();
- AuthorList.Add("Mahesh Chand");
- AuthorList.Add("Praveen Kumar");
- AuthorList.Add("Raj Kumar");
- AuthorList.Add("Nipun Tomar");
- AuthorList.Add("Dinesh Beniwal");
- AuthorList.Add("Mahesh Chand");
- AuthorList.Add("Praveen Kumar");
- AuthorList.Add("Raj Kumar");
- AuthorList.Add("Nipun Tomar");
- AuthorList.Add("Dinesh Beniwal");
-
- string[] authorArray = new string[15];
-
- AuthorList.CopyTo(authorArray);
-
- AuthorList.CopyTo(authorArray, 4);
-
-
- AuthorList.CopyTo(2, authorArray, 3, 4);
- foreach (string author in authorArray)
- {
- Console.WriteLine(author);
- }
- }
- }
- }
The output from above code listing is shown in below figure.