Get the index of foreach loop

Select method (Sysmte.Linq namespace) has an overload to return the loop index.

With calling this overload, the index of the loop can be get without a temporary variable.

C# code

var array = new[] { "A", "B", "C" };

foreach (var item in array.Select((v, i) => new { v, i }))
{
    Console.WriteLine("value = {0}, index = {1}", item.v, item.i);
}

Result

value = A, index = 0
value = B, index = 1
value = C, index = 2