Posts Using foreach with index in C#
Post
Cancel

Using foreach with index in C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// foreach with a "manual" index
int index = 0;
foreach (var item in collection)
{
    DoSomething(item, index);
    index++;
}

// normal for loop
for (int index = 0; index < collection.Count; index++)
{
    var item = collection[index];
    DoSomething(item, index);
}

foreach (var (item, index) in collection.WithIndex())
{
    DoSomething(item, index);
}

ref - https://thomaslevesque.com/2019/11/18/using-foreach-with-index-in-c/

origin - https://www.pipiscrew.com/?p=15811 using-foreach-with-index-in-c

This post is licensed under CC BY 4.0 by the author.
Contents

Trending Tags