Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
241 views
in Technique[技术] by (71.8m points)

.net - 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 List<T> of structs. I'm trying to iterate through the List 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)
{
    Compare(member, member.next);
}
question from:https://stackoverflow.com/questions/2404193/how-can-i-access-the-next-value-in-a-collection-inside-a-foreach-loop-in-c

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can't. Use a for instead

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...