C# - foreach 显示奇怪的行为/工作没有问题
今天我编写了一个使用两个嵌套 foreach 循环的函数。看到它没有按预期工作后,我调试了它。但我没有看到错误,并且不认为简单的错误会导致我注意到的行为。
该部分如下所示:
foreach(MyClass cItem in checkedListBoxItemList.Items)
{
foreach(MyClass cActiveItem in ActiveItemList)
{
if (cActiveItem.ID == cItem.ID) /*...check checkbox for item...*/;
}
}
可以说,checkedListBoxItemList.items 包含 4 个 MyClass 类型的项目,而 ActiveItemList 是一个 List<我的班级>有 2 件物品。
调试器跳转到外部 foreach,到达内部 foreach,执行 if 2 次(每个 cActiveItem 一次)并到达外部 foreach 的末尾。现在,调试器按预期跳回外部 foreach 的头部。但调试器并没有开始第二轮外部 foreach,而是突然跳转到 MyClass.ToString() 方法。 我可以单步执行此方法 4 次(checkedListBoxItemList.Items 中的项目数) 然后……什么也没有。 Visual Studio 向我显示了我的 Windows 窗体,并且 foreach 没有继续。
当将代码更改为
int ListCount = checkedListBoxItemList.Items.Count;
for(int i=0; i<ListCount; i++)
{
MyClass cItem = checkedListBoxItemList.Items[i] as MyClass;
foreach(MyClass cActiveItem in ActiveItemList)
{
if (cActiveItem.ID == cItem.ID) /*...check checkbox for item...*/;
}
}
一切正常且符合预期时。 我把这个问题展示给一位同事,但他也不明白,发生了什么。我不明白为什么调试器会跳转到 MyClass.ToString() 方法。我使用F10单步执行,因此无需离开该功能。甚至,如果有原因,为什么 foreach 循环不继续?
我正在使用 Visual Studio 2010,如果这有什么问题的话。
请告诉我发生了什么事。谢谢。
Today I coded a function that uses two nested foreach loops. After seeing, that it did not work like expected, i debugged it. But I dont see an error, and dont think a simple error can cause the behavior i have noticed.
The part looks like this:
foreach(MyClass cItem in checkedListBoxItemList.Items)
{
foreach(MyClass cActiveItem in ActiveItemList)
{
if (cActiveItem.ID == cItem.ID) /*...check checkbox for item...*/;
}
}
Lets say, checkedListBoxItemList.items holds 4 items of type MyClass, and ActiveItemList is a List< MyClass > with 2 Items.
The debugger jumps into the outer foreach, reaches inner foreach, executes the if 2 times (once per cActiveItem) and reaches the end of the outer foreach.Now, the debugger jumps back to the head of the outer foreach as it should. But instead of starting the second round of the outer foreach, the debugger suddenly jumps into the MyClass.ToString() method.
I can step through this method 4 times (number of items in checkedListBoxItemList.Items)
and then ... nothing. Visual Studio shows me my windows form, and the foreach is not continued.
When changing the code to
int ListCount = checkedListBoxItemList.Items.Count;
for(int i=0; i<ListCount; i++)
{
MyClass cItem = checkedListBoxItemList.Items[i] as MyClass;
foreach(MyClass cActiveItem in ActiveItemList)
{
if (cActiveItem.ID == cItem.ID) /*...check checkbox for item...*/;
}
}
everything works fine and as supposed.
I showed the problem to a collegue, but he also didnt understand, what happened. I dont understand why the debugger jumps into the MyClass.ToString() method. I used F10 to step through, so no need to leave the function. And even, if there is a reason, why isnt the foreach loop continued?
Im using Visual Studio 2010, if this is of any matter.
Please tell me what happened. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当迭代一个集合时(使用foreach),你正在迭代的集合是不允许改变的;但是当您检查匹配项的复选框时,外部循环的集合 (
checkedListBoxItemList.Items
) 会发生变化,并且随后引发的错误可能会被吞没在某个地方。这或多或少解释了为什么您突然进入ToString
方法并且不继续循环。当您使用
for
语句进行迭代时,您没有该限制,因为在开始迭代时没有对集合的引用。希望这能解释。
When iterating a collection (using a foreach), the collection you are iterating is not allowed to change; but when you check the checkbox for the matching item, the collection of the outer loop (
checkedListBoxItemList.Items
) changes and the consequent error that is thrown is probably swallowed somewhere. That more or less explains why you suddenly go into theToString
method and don't continue the loop.When you use a
for
statement to iterate, you don't have that restriction as there is no reference to the collection at the moment you start iterating.Hope this explains.