EN
C# / .NET - create new IEnumerable object from existing List
7
points
In this article, we would like to show you how to create new IEnumerable object from existing List in C#.
Practical example:
using System;
using System.Collections;
using System.Collections.Generic;
class EnumerableCollection<T> : IEnumerable<T>
{
private IEnumerable<T> collection;
public EnumerableCollection(IEnumerable<T> collection)
{
this.collection = collection;
}
public IEnumerator<T> GetEnumerator()
{
return this.collection.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.collection.GetEnumerator();
}
}
public class Program
{
public static void Main()
{
List<int> list = new List<int> { 1, 2, 3 };
IEnumerable<int> enumerable = new EnumerableCollection<int>(list);
foreach (int i in enumerable)
Console.WriteLine(i);
}
}
Output:
1
2
3