John Skeet sometimes does a LINQ demo for live performances to explain how this works. Imagine you have three people on stage. On the left, we have one guy who has a shuffled deck of cards. In the middle, we have one guy who only passes on red cards, and on the right, we have a guy who wants cards.
The guy rightfully forces the guy in the middle. The guy in the middle deflates the guy on the left. The guy on his left hand is the guy in the middle of the map. If it is black, the guy in the middle throws him to the floor and shuts him up again until he receives a red card, which he then directs to the guy on the right. Then the guy rightly again forces the guy in the middle.
This continues until the guy on the left has no cards left.
The deck did not go from start to finish more than once. . However, both the guy on the left and the guy in the middle processed 52 cards, and the guy on the right hand 26 cards. There were only 52 + 52 + 26 operations on the cards, but the deck was only looped.
Your version of LINQ and the continue version are one and the same; if you have
foreach(var card in deck) { if (card.IsBlack) continue; ... use card ...
then there are 52 operations that extract each card from the deck, 52 operations that verify that each card is black, and 26 operations that operate on the red card. The exact same thing.
Eric Lippert Jan 30 '12 at 23:29 2012-01-30 23:29
source share