Addie Addie - 1 year ago 52
C# Question

How can I access the next value in a collection inside a foreach loop in C#?

I'm working in C# and with a sorted

of structs. I'm trying to iterate through the
and for each iteration I'd like to access the next member of the list. Is there a way to do this?

Pseudocode example:

foreach (Member member in List)

Answer Source

You can't. Use a for instead

for(int i=0; i<list.Count-1; i++)
   Compare(list[i], list[i+1]);