Skip to content

IndexedEnumerations

David Arno edited this page May 25, 2017 · 1 revision

Indexed IEnumerable<T>

Indexed() extension method that provides IEnumerable<(int index, T item)>


Sometimes, when enumerating a collection, it is useful to have access to both the item itself, as well as the element's index in the collection. Succinc<T> provides an Indexed() extension method that provides an IEnumerable<(int index, T item)> enumeration:

var list = new List<int> { 1, 2, 3 };
var indexedList = list.Indexed();
foreach (var (index, item) in indexedList)
{
    Console.WriteLine($"Item {index} = {item}.");
}
Clone this wiki locally