【C#】インデックス付きforeach
ひさびさC#ネタ
IEnumerable拡張です。
public static void ForEachWithIndex<T> (this IEnumerable<T> source, Action<T, int> action) { foreach (var x in source.Select((item, index) => new {item, index})) action (x.item, x.index); }
使用例
foo,barデビューしました。
var array = new string[]{ "foo", "bar"}; array.ForEachWithIndex ((x, i) => { // なんか処理 });