Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I tried to look around for some documentation on exactly how <code>For Each</code> behaves, but I couldn't find it.</p> <p>My theory is that using the <code>For Each</code> statements copies the object in the list to another spot in memory and then copies it back into the list when each iteration of the loop ends.</p> <p>Another possibility is that it calls the constructor at the start of every iteration and then deconstructs and calls the constructor again to reset for the next iteration.</p> <p>I'm not sure on either of these theories, but the major difference between 3 and (1 or 2) is the lack of <code>For Each</code>.</p> <p>EDIT: Found some documentation at <a href="http://msdn.microsoft.com/en-us/library/5ebk1751.aspx" rel="nofollow noreferrer">MSDN</a>.</p> <p>Here's an excerpt:</p> <blockquote> <p>When execution of the For Each...Next loop begins, Visual Basic verifies that group refers to a valid collection object. If not, it throws an exception. Otherwise, it calls the MoveNext method and the Current property of the enumerator object to return the first element. If MoveNext indicates that there is no next element, that is, if the collection is empty, then the For Each loop terminates and control passes to the statement following the Next statement. Otherwise, Visual Basic sets element to the first element and runs the statement block. </p> </blockquote> <p>So overall it sounds like <code>For Each</code> is more "managed" and does a lot of overhead to make sure everything matches up. As a result, it's slower.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload