What is the simplest way to get generic IEnumerable<T>
from System.Collections.IEnumerable
(which contains only T type elements) ?
Something say to me that there should be simple (avoiding LINQ) standard wrapper, but I can't find it.
What is the simplest way to get generic IEnumerable<T>
from System.Collections.IEnumerable
(which contains only T type elements) ?
Something say to me that there should be simple (avoiding LINQ) standard wrapper, but I can't find it.
Simplest and shortest is with LINQ. You can enumerate it without LINQ:
foreach (T item in IEnumerableCollection)
and this is pretty much what .Cast<T>()
does:
static IEnumerable<TResult> CastIterator<TResult>(IEnumerable source) {
foreach (object obj in source) yield return (TResult)obj;
}