改变Foreach订单? [英] Changing Foreach Order?
本文介绍了改变Foreach订单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
解决方案有没有办法通过从头到尾的列表来进行搜索,而不是从开始到结束(最好不用重新排序列表)。
使用System.Linq;
foreach(source.reverse()中的var item)
{
...
}
编辑:如果您特别处理 List< T>
。该类定义了自己的 Reverse
方法,其签名与 Enumerable.Reverse
扩展方法不同。在这种情况下,您需要将变量引用提升到 IEnumerable< T>
:
使用System.Linq;
$ b foreach(var item in list.AsEnumerable()。Reverse())
{
...
}
Is there anyway to foreach through a list from the end to the beginning rather than the beginning to then end (preferably without reordering the list).
解决方案
using System.Linq;
foreach(var item in source.Reverse())
{
...
}
Edit: There is one more step if you are dealing specifically with a List<T>
. That class defines its own Reverse
method whose signature is not the same as the Enumerable.Reverse
extension method. In that case, you need to "lift" the variable reference to IEnumerable<T>
:
using System.Linq;
foreach(var item in list.AsEnumerable().Reverse())
{
...
}
这篇关于改变Foreach订单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文