Convert IEnumerable Into List in C#

Abdullahi Salawudeen Mar 29, 2022
  1. Use ToList() to Convert Data From IEnumerable Into List in C#
  2. Use ToList() to Convert Data From Array to List in C#
  3. Use ToArray() to Convert Data From List to Array in C#
  4. Use AsEnumerable() to Convert Data From List Into IEnumerable in C#
Convert IEnumerable Into List in C#

This article will illustrate converting data from IEnumerable to list in C#.

Use ToList() to Convert Data From IEnumerable Into List in C#

IEnumerable is an interface contained in the System.Collections.Generic namespace. Like every other interface, it exposes a method.

This case exposes the enumerator method, which supports iteration or loop through generic and non-generic lists, including LINQ queries and Arrays.

IEnumerable contains only the GetEnumerator method, which returns an IEnumerator object.

public interface IEnumerable<out T> : System.Collections.IEnumerable

The values returned by the IEnumerable interface are read-only. This implies that manipulation is limited to these data.

The ToList() method in C# is an alternate method to manipulate these data. Elements in a list in C# can be added, removed, ordered, and re-arranged.

There are so many operations that can be performed on a list compared to the IEnumerable values.

The C# List class represents a collection of strongly typed objects that can be accessed by index. ToList() function or method is in the System.Linq namespace (Language-Integrate Query).

The ToList operator in LINQ takes the element from the given source and returns a new list. The input would be converted to a type list.

The ToList() method returns a list of string instances. The ToList() function can be invoked on an array reference or IEnumerable values and vice versa.

Further discussion is available via this reference.

List elements can be accessed or manipulated as follows:

// an array of 4 strings
string[] animals = { "Cow", "Camel", "Elephant" };

//creating a new instance of a list
List<string> animalsList = new List<string>();

//use AddRange to add elements
animalsList.AddRange(animals);

// declaring a list and passing array elements into the list
List<string> animalsList = new List<string>(animals);

//Adding an element to the collection
animalsList.Add("Goat");
Console.WriteLine(animalsList[2]); // Output Elephant, Accessing elements of a list

// Collection of new animals
string[] newAnimals = { "Sheep", "Bull", "Camel" };

// Insert array at position 3
animalsList.InsertRange(3, newAnimals);

// delete 2 elements at starting with element at position 3
animalsList.RemoveRange(3, 2);

The namespace must be imported to use the ToList function as follows:

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

Below is the syntax of the ToList() method.

List<string> result = countries.ToList();

Below is an example code to convert IEnumerable to list in C#.

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

namespace TestDomain
{
    class Program
    {
        public static void Main(String[] args)
        {
            IEnumerable<int> testValues = from value in Enumerable.Range(1, 10) select value;
            List<int> result = testValues.ToList();
            foreach (int a in result)
            {
                Console.WriteLine(a);
            }
        }
    }
}

Output:

1
2
3
4
5
6
7
8
9
10

Use ToList() to Convert Data From Array to List in C#

Below is an example code to convert an array to a list in C#.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ArrayToListApp
{
    class Program
    {
        static void Main(string[] args)
        {
            //create an array of African countries of type string containing the collection of data
            string[] countries = { "Nigeria", "Ghana", "Egypt", "Liberia", "The Gambia", "Morocco", "Senegal" };
            //countries.ToList() convert the data collection into the list.
            List<string> result = countries.ToList();
            //foreach loop is used to print the countries
            foreach (string s in result)
            {
                Console.WriteLine(s);
            }
        }
    }
}

Output:

Nigeria
Ghana
Egypt
Liberia
The Gambia
Morocco
Senegal

Use ToArray() to Convert Data From List to Array in C#

Below is an example code to convert from list to array in C#.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ListToArrayApp
{
    class Program
    {
        static void Main(string[] args)
        {
            //create an array of African countries of type string containing the collection of data
            IEnumerable<int> testValues = from value in Enumerable.Range(1, 10) select value;

            //countries.ToList() convert the data collection into the list.
            List<int> result = testValues.ToList();
            int[] array = result.ToArray();//convert string to array.
            //foreach loop is used to print the countries
            foreach (int i in array)
            {
                Console.WriteLine(i);
            }
        }
    }
}

Output:

1
2
3
4
5
6
7
8
9
10

Use AsEnumerable() to Convert Data From List Into IEnumerable in C#

Below is an example code to convert IEnumerable to list and back to IEnumerable.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ListToArrayApp
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> list = new List<int>();
            IEnumerable enumerable = Enumerable.Range(1, 5);

            foreach (int item in enumerable) {
                list.Add(item);
            }
            Console.WriteLine("Output as List");
            foreach (var item in list) {
                Console.WriteLine(item);
            }
            Console.WriteLine("Output as Enumerable");
            //using the AsEnumerable to	convert list back to Enumerable
            IEnumerable resultAsEnumerable = list.AsEnumerable();
            foreach (var item in resultAsEnumerable) {
                Console.WriteLine(item);
            }
        }
    }

}

Output:

Output as List
1
2
3
4
5
Output as Enumerable
1
2
3
4
5
Abdullahi Salawudeen avatar Abdullahi Salawudeen avatar

Abdullahi is a full-stack developer and technical writer with over 5 years of experience designing and implementing enterprise applications. He loves taking on new challenges and believes conceptual programming theories should be implemented in reality.

LinkedIn GitHub

Related Article - Csharp List

Related Article - Csharp IEnumerable