批准列表后,foreach循环不会退出 [英] foreach loop does not exit after approval of list

查看:101
本文介绍了批准列表后,foreach循环不会退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,与主题相同。

我有两个嵌套循环

So, same as the topic.
I have two nested loops

foreach (CData x in listData) // count = 5 {list info 1 : 3,list info 1 : 6}
{
   foreach(Data y in x.listInfo)//count = 8
   {
      //do work
      //when count == 3, not exit, x.listinfo.count is change to 6 ???
      //when count ==6, continue loop with info == 6 from loop parent
   }
}





运行代码时,循环子结束时x.listInfo会发生变化。所以,它继续使用循环父级。

有什么问题???

可以帮助我???

非常感谢它

抱歉我的英语



When run code, x.listInfo is change when loop child is end. so, it's continue work with loop parent.
What's the problem???
can help me ???
Thanks so much for it's
and sorry about my english

推荐答案

很难准确地解决你想要做的事情,但也有一些事情这可能有所帮助:

1) foreach 循环继续集合中的所有元素,除非你使用 break (在循环后立即继续)或 return (完全退出例程)或发生错误(在这种情况下最近的 try ... catch 块将获得执行)。你不能动态地重新排序元素,或者根据现有元素跳过元素。

2)你不能从内部循环中影响外部循环。所以你不能强迫外循环跳过元素或从内循环中移动到 continue 的下一个元素 - 它将影响内部循环,而不是外部循环。



您可能需要一组来代替循环,或者您需要提供一些变量在循环之间进行通信 - 如果不确切知道你要做什么,很难确定。
It's difficult to work out exactly what you are trying to do from that, but there are some things that may help:
1) foreach loops continue for all elements in the collection, unless you specifically exit the loop using a break (which continues immediately after the loop) or a return (which exits the routine entirely) or an error occurs (in which case the "closest" try...catch block will get the execution). You cannot reorder elements on the fly, or "skip" elements based on existing ones.
2) You cannot affect the outer loop from within the inner loop. So you cannot force the outer loop to "skip" elements or move to the next element with a continue from within the inner loop - it will affect the inner, not the outer loop.

It may be that you need a set of for loops instead, or you need to provides some variables to communicate between the loops - without knowing exactly what you are trying to do it is difficult to be sure.


这篇关于批准列表后,foreach循环不会退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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