Convert an IEnumerable to a List in C#

This article will introduce a method to convert an IEnumerable to a list in C#.

  • Use the ToList() method

Use the ToList() Method to Convert an IEnumerable to a List in C#

In C#, we can use the ToList() method of Linq class to convert an IEnumerable to a list. The correct syntax to use this method is as follows

Enumerable.ToList(source);

The method ToList() has one parameter only. The detail of its parameter is as follows.

Parameters Description
source mandatory This is the IEnumerable that we want to convert to a list.

This function returns a list representing the elements of the given IEnumerable.

The program below shows how we can use ToList() method to convert an IEnumerable to a list.

using System;
using System.Collections.Generic;
using System.Linq;

class StringToByteArray {
  static void Main(string[] args) {
    IEnumerable < int > enumerable = Enumerable.Range(1, 50);
    List < int > mylist = enumerable.ToList();
    Console.WriteLine("The List is:");
    foreach(int length in mylist) {
      Console.WriteLine(length);
    }

  }
}

Output:

The List is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
Write for us
DelftStack articles are written by software geeks like you. If you also would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Csharp IEnumerable

  • Convert IEnumerable Into List in C#
  • Convert List to IEnumerable in C#
  • Related Article - Csharp List

  • Convert IEnumerable Into List in C#
  • Convert List to IEnumerable in C#