Languages
[Edit]
EN

C# / .NET - cast List to IEnumerable object

3 points
Created by:
Kelly
394

In this article, we would like to show you how to cast List to IEnumerable object in C#.

Quick solution:

List<int> list = new List<int> { 1, 2, 3 };

IEnumerable<int> enumerable = list;

 

Practical example

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        List<int> list = new List<int> { 1, 2, 3 };

        IEnumerable<int> enumerable = list;

        foreach (int i in enumerable)
        {
            Console.WriteLine(i);
        }
    }
}

Output:

1
2
3

See also

  1. C# / .NET - create new IEnumerable object from existing List

References

  1. IEnumerable Interface (System.Collections) | Microsoft Docs
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join