如何在 C# 中的 foreach 循环内访问集合中的下一个值? [英] How can I access the next value in a collection inside a foreach loop in C#?

查看:16
本文介绍了如何在 C# 中的 foreach 循环内访问集合中的下一个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 C# 并使用已排序的 List 结构.我正在尝试遍历 List 并且对于每次迭代,我想访问列表的下一个成员.有没有办法做到这一点?

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?

伪代码示例:

foreach (Member member in List)
{
    Compare(member, member.next);
}

推荐答案

你不能.使用 for 代替

You can't. Use a for instead

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

这篇关于如何在 C# 中的 foreach 循环内访问集合中的下一个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆