Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSHARP

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);
}
Source by thomaslevesque.com #
 
PREVIOUS NEXT
Tagged: #item #index
ADD COMMENT
Topic
Name
2+1 =