📌  相关文章
📜  在 c# 代码示例中同时获取项目和索引

📅  最后修改于: 2022-03-11 14:49:17.339000             🧑  作者: Mango

代码示例1
// 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);
}