A disadvantage of loop for, foreach, while in C #

Source: Internet
Author: User

First, we create a vs winform project and create a new form. There is a Treeview and button control in the form, and 10 nodes are added to the Treeview. The names are N0, N1, N2 ...., the text values are node0, node1, node2 ......, the function of the button is to clear the nodes in the Treeview,

(1)

Figure (1)

Then, add the code to clear nodes in the Click Event of the button, and use loops to implement this function. Use the for, foreach, and while loops respectively to implement this function:

① For Loop

For (INT I = 0; I <trvcirtest. nodes. Count; I ++)
{
Trvcirtest. nodes. Remove (trvcirtest. nodes [I]);
}

② Foreach Loop

Foreach (treenode node in trvcirtest. nodes)
{
Trvcirtest. nodes. Remove (node );
}

 

③ WHILE LOOP

Int I = 0;
While (trvcirtest. nodes. Count> 0)
{
Trvcirtest. nodes. Remove (trvcirtest. nodes [I]);
I ++;
}

When an exception is caught by the try {} catch {} module, the following results are displayed:

① For Loop

No exception is thrown, but only half of nodes can be cleared at each button.

② Foreach Loop

Each time you click a button, only half of nodes can be cleared and an (2) exception is thrown.

Figure (2)

③ WHILE LOOP

Each time you click a button, only half of the nodes can be cleared, and an (3) exception is thrown.

Figure (3)

In normal thinking, the program traverses to the first node, deletes the first node, and deletes the second node when traversing to the second node ....., we should be able to delete all the nodes. Why is this happening or even throw an exception?

Now, we can find through breakpoint tracking:

① For Loop

When I = 0, the value of trvcirtest. nodes. Count is 10,

When I = 1, the value of trvcirtest. nodes. Count is 9,

When I = 2, the value of trvcirtest. nodes. Count is 8,

......

When I = 4, the value of trvcirtest. nodes. Count is 6,

When I = 5, the value of trvcirtest. nodes. Count is 5, 5 <5? False: jumps out of the loop.

② Foreach Loop

For the first traversal, remove node0,

Traverse the second time, remove node2,

Traverse the third time, remove node4,

......

For the fifth traversal, remove node10. If node10 does not exist, an exception is thrown.

③ WHILE LOOP

This is similar to the for loop, but the while loop does not jump out. When the number of loops reaches 5th, trvcirtest. the value of count is 5, trvcirtest. remove (trvcirtest. nodes [5]), an exception is thrown.

 

Analysis summary:

From the above debugging, we can see that the loop pointer adopts the backward-moving method. Every cycle is performed, and the pointer moves one bit backward. However, when an element is deleted, the pointer does not stop because of the change of the element, and it is not likely to move backwards. However, the combination of elements is like an hourglass, and the sand below leaks out, the sand above will immediately fill in the original space occupied by the sand. This is a bit like a syringe. The element is like the liquid in the syringe. The circulating Pointer Points to the scale. The pointer increases from small to large, but it does not stop or decrease because of the decrease of the liquid.

Finally, Ms is recommended to consider this issue. Can we monitor the changes of these elements and change the pointer? Of course, this problem is only for the current program or similar programs. I don't know if such a change in MS will cause other problems, just for reference!

In addition, the attached solution is actually very simple:

① For Loop

For (INT I = 0; I <trvcirtest. nodes. Count; I ++)
{
Trvcirtest. nodes. Remove (trvcirtest. nodes [0]);

② Foreach Loop

Foreach (treenode node in trvcirtest. nodes)
{
Trvcirtest. nodes. Remove (trvcirtest. nodes [0]);
}

 

③ WHILE LOOP

While (trvcirtest. nodes. Count> 0)
{
Trvcirtest. nodes. Remove (trvcirtest. nodes [0]);

}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.