How do you get the index of the current iteration of a foreach loop?

Is there some rare language construct I haven’t encountered (like the few I’ve learned recently, some on Stack Overflow) in C# to get a value representing the current iteration of a foreach loop?

For instance, I currently do something like this depending on the circumstances:

int i = 0;
foreach (Object o in collection)
{
    // ...
    i++;
}

3
35

Leave a Comment