My solution involves a simple Pair<X, Y> class I created for general utility, and which is operationally essentially the same as the framework class KeyValuePair<X, Y>. Then I created a couple extension functions for IEnumerable<T> called Ordinate (from the set theory term "[ordinal][1]").
These functions will return for each item a Pair object containing the index, and the item itself.
public static IEnumerable<Pair<Int32, X>> Ordinate<X>(this IEnumerable<X> lhs)
{
return lhs.Ordinate(0);
}
public static IEnumerable<Pair<Int32, X>> Ordinate<X>(this IEnumerable<X> lhs, Int32 initial)
{
Int32 index = initial - 1;
return lhs.Select(x => new Pair<Int32, X>(++index, x));
}
[1]:
[To see links please register here]