Convert List To IEnumerable In C# - AsEnumerable Method In C#

Sameer Saini April 05, 2022
Convert List To IEnumerable In C# - AsEnumerable Method In C#

In this blog post, we will see how we can convert a List to IEnumerable in C#.

A List in C# already implements an IEnumerable but you may still feel the need to convert a List to IEnumerable in C#.

Let's see how we can do this.

 

 

C# List To IEnumerable Using .AsEnumerable() Method

Using Linq, we can use the .AsEnumerable() method in C# which is available to us on Lists.

Using this .AsEnumerable method, we can convert a list to Ienumerable.

Let's look at a code example.

var listOfColors = new List { "orange", "red", "green", "pink" }; IEnumerable colors = listOfColors.AsEnumerable(); foreach (var color in colors) { Console.WriteLine(color); }

 

Result:

orange red green pink

As you can see that we converted a list to IEnumerable in C# using the .AsEnumerable method.