Is there a good way to enumerate through only a subset of a Collection in C#? That is, I have a collection of a large number of objects (say, 1000), but I\'d like to enumerate
Adapting Jared's original code for .Net 2.0:
IEnumerable GetRange(IEnumerable source, int start, int end)
{
int i = 0;
foreach (T item in source)
{
i++;
if (i>end) yield break;
if (i>start) yield return item;
}
}
And to use it:
foreach (T item in GetRange(MyCollection, 250, 340))
{
// do something
}