Try the following
foreach ( var item in numbers.Select( (x,i) => new { Index = i, Value = x })) {
var index = item.Index;
var value = item.Value;
...
}
There is an overload of select which passes down the index of the item. This code will create a new anonymous type for every item which includes both the index and the value.
Here's an alternate way which makes the syntax slightly more readable.
public static void ForEach<T>(this IEnumerable<T> source, Action<T,int> del) {
int i = 0;
foreach ( var cur in source ) {
del(cur, i);
i++;
}
}
numbers.ForEach( (x,i) =>
{
// x is the value and i is the index
}
This doesn't add a whole lot over the define a local and increment it manually solution. Is there a particular reason you don't want to do it that way?