【发布时间】:2020-09-11 03:40:09
【问题描述】:
我有以下代码:
int index = currentIndex;
do
{
if (index == list.Count - 1)
{
// At the end, cycle round to the start
index = -1;
}
index++;
}
while (list[index] satisfies some condition);
// Do something with list[index]
currentIndex = index;
这显然有效,而且效率并不低。
但是,这段代码被调用了好几次,我想知道它是否可以通过某种方式变得更高效或更简洁。
【问题讨论】:
-
我认为这取决于您在循环中所做的事情,这将决定如何使索引回到-1。你能不循环列表本身,这样你就不必重置索引吗?
-
@Jawad 这是通过系统调用获得的文件夹/目录名称列表。我可以把它放到一个链表中,但我不确定这会更快。
标签: c# arrays loops while-loop