dictionary with index C# code example
Example 1: get both item and index in c#
// add this to your namespace
public static IEnumerable<(T item, int index)> WithIndex<T>(this IEnumerable<T> source)
{
return source.Select((item, index) => (item, index));
}
//do something like this
foreach (var (item, index) in collection.WithIndex())
{
DoSomething(item, index);
}
Example 2: how to get element dictionary key in c# by index
dictionary.ElementAt(index).Key