是否有可能通过循环"寻找"?基于某些条件,我想做一个"继续到位",以快进循环.像这样:
for(var thing in things)
{
// Do stuff
if(something)
{
// Move iteration forward until the iteration object ("thing") meets the right condition
while(true)
{
// Move the iteration forward...somehow
[Missing code goes here]
if(thing.Property == somevalue)
{
break;
}
}
}
// Do more stuff on the new value of "thing"
}
Run Code Online (Sandbox Code Playgroud)
我可以使用continue,但我不想回到循环的顶部.我想循环通过枚举器中的对象,然后从我离开的地方继续前进.
我猜这是不可能的.如果没有,那么模仿我想做什么的最佳逻辑是什么.
小智 5
您是否考虑使用标准for循环而不是foreach您正在使用的循环?
例如
var thing;
for (int i = 0; i < things.Length; i++)
{
thing = things[i];
//do stuff
if(something)
{
while([thing doesn't meet condition] && i < things.Length - 1 )
{
thing = things[++i];
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果你在(i++)之前看过增量器,++i可能看起来很奇怪.它只是i 在你使用它之前增加.因此,如果您进入while循环things[5],thing将设置为things[6].while如果无法加载任何对象,循环也会中断.
| 归档时间: |
|
| 查看次数: |
525 次 |
| 最近记录: |