Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to copy a List collection to an array?
To copy a C# List collection to an array, you can use several methods. The most common approaches are the CopyTo() method, the ToArray() method, or creating an array with the List constructor.
Syntax
Following is the syntax for using CopyTo() method −
list.CopyTo(array); list.CopyTo(array, arrayIndex);
Following is the syntax for using ToArray() method −
T[] array = list.ToArray();
Using CopyTo() Method
The CopyTo() method copies all elements from the List to an existing array starting at the specified array index −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
List list1 = new List();
list1.Add("One");
list1.Add("Two");
list1.Add("Three");
list1.Add("Four");
Console.WriteLine("Original List:");
foreach(string value in list1) {
Console.WriteLine(value);
}
string[] arr = new string[20];
list1.CopyTo(arr);
Console.WriteLine("After copying to array:");
foreach(string value in arr) {
if(value != null)
Console.WriteLine(value);
}
}
}
The output of the above code is −
Original List: One Two Three Four After copying to array: One Two Three Four
Using ToArray() Method
The ToArray() method creates a new array containing all elements from the List −
using System;
using System.Collections.Generic;
using System.Linq;
public class Demo {
public static void Main() {
List numbers = new List {10, 20, 30, 40, 50};
Console.WriteLine("Original List:");
foreach(int num in numbers) {
Console.WriteLine(num);
}
int[] numberArray = numbers.ToArray();
Console.WriteLine("Converted Array:");
foreach(int num in numberArray) {
Console.WriteLine(num);
}
Console.WriteLine("Array Length: " + numberArray.Length);
}
}
The output of the above code is −
Original List: 10 20 30 40 50 Converted Array: 10 20 30 40 50 Array Length: 5
Using CopyTo() with Index Position
You can specify the starting index in the destination array where copying should begin −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
List fruits = new List {"Apple", "Banana", "Orange"};
string[] fruitArray = new string[6];
fruitArray[0] = "Start";
fruits.CopyTo(fruitArray, 2);
Console.WriteLine("Array after copying with index:");
for(int i = 0; i
The output of the above code is −
Array after copying with index:
Index 0: Start
Index 2: Apple
Index 3: Banana
Index 4: Orange
Comparison of Methods
Method
Creates New Array
Requires Pre-sized Array
Can Specify Start Index
CopyTo()
No
Yes
Yes
ToArray()
Yes
No
No
Conclusion
Converting a List to an array in C# can be achieved using CopyTo() for copying to existing arrays or ToArray() for creating new arrays. Choose ToArray() for simplicity when you need a new array, or CopyTo() when you need more control over the destination array.
