Possible Duplicate:
C# foreach with index
I enjoy working the simplicity of foreach in C#, but I often need an index. Is there any way to combine these elegantly into one simple structure without doing the following:
for(var i = 0; i<items.Count; i++) {
var item = items[i];
Or
int i = 0;
foreach(var item in items) {
i++;
I’d imagine something neat like:
foreach(var item in items;int index) {
yes,
there’s a neat extension method that i found here on SO:
foreach with index
works a bit like this (verbatim from previous answer):
and is used like:
or: