Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - foreach showing strange behavior / for working with no problem

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.

like image 850
Marks Avatar asked Apr 28 '10 14:04

Marks


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

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 the ToString method and don't continue the loop.

When you use a forstatement 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.

like image 160
Thomas Avatar answered Oct 15 '22 07:10

Thomas