get both item and index in c# code example
Example 1: c# get index of item in list
Array.IndexOf(arrName, searchingFor)
Example 2: get both item and index in c#
// add this to your namespace
public static IEnumerable<(T item, int index)> WithIndex(this IEnumerable source)
{
return source.Select((item, index) => (item, index));
}
//do something like this
foreach (var (item, index) in collection.WithIndex())
{
DoSomething(item, index);
}