EN
C# / .NET - cast List to IEnumerable object
3
points
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