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
C# program to create a List with elements from an array
In C#, you can easily create a List<T> with elements from an existing array. The List<T> constructor accepts an IEnumerable<T> parameter, which allows you to initialize the list directly with array elements.
Syntax
Following is the syntax to create a list from an array −
List<T> listName = new List<T>(arrayName);
You can also use the ToList() extension method −
List<T> listName = arrayName.ToList();
Using List Constructor
The most straightforward way is to pass the array directly to the List<T> constructor −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
int[] val = new int[5];
// integer elements
val[0] = 15;
val[1] = 25;
val[2] = 40;
val[3] = 58;
val[4] = 70;
List<int> myList = new List<int>(val);
Console.WriteLine("Elements...");
foreach(int res in myList) {
Console.WriteLine(res);
}
// count integer elements
Console.WriteLine("Number of elements: " + myList.Count);
}
}
The output of the above code is −
Elements... 15 25 40 58 70 Number of elements: 5
Using Array Initializer with List
You can also create a list directly from an array initializer −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
int[] numbers = {10, 20, 30, 40, 50};
List<int> numberList = new List<int>(numbers);
Console.WriteLine("List created from array initializer:");
foreach(int num in numberList) {
Console.WriteLine(num);
}
Console.WriteLine("Total elements: " + numberList.Count);
}
}
The output of the above code is −
List created from array initializer: 10 20 30 40 50 Total elements: 5
Using ToList() Extension Method
The ToList() method from System.Linq provides another convenient way to convert arrays to lists −
using System;
using System.Collections.Generic;
using System.Linq;
public class Demo {
public static void Main() {
string[] cities = {"New York", "London", "Paris", "Tokyo"};
List<string> cityList = cities.ToList();
Console.WriteLine("Cities converted to List:");
foreach(string city in cityList) {
Console.WriteLine(city);
}
Console.WriteLine("List count: " + cityList.Count);
}
}
The output of the above code is −
Cities converted to List: New York London Paris Tokyo List count: 4
Comparison of Methods
| Method | Namespace Required | Performance | Usage |
|---|---|---|---|
| List Constructor | System.Collections.Generic | Faster | Direct initialization |
| ToList() Method | System.Linq | Slightly slower | More readable, chainable |
Conclusion
Creating a List<T> from an array in C# can be done using the List constructor or the ToList() extension method. The constructor approach is more direct and performs better, while ToList() offers better readability and is useful in LINQ operations.
