How to count without a counter in the selection?

Sometimes, during the development phase, I need to see, for example, the name changes for each element created or just wants to distinguish the names of different instances in the array. This behavior is strictly for the development stage and should be applied only for a short period of time (hence there is no need for a long-term solution - Q & D is just fine).

I usually entered the counter as follows, but it just struck me that there might be a better way. Basically, I want to imitate the behavior of the counting variable for for without actually entering it (staying in foreach).

int counter = 1; IEnumerable<Typo> result = originals.Select(original => new Thingy { Name = "Hazaa" + counter++ }); 
+5
source share
1 answer

Use this Select Overload

 IEnumerable<Typo> result = originals.Select((original, counter) => new Thingy { Name = "Hazaa" + (counter + 1) }); 
+12
source

All Articles