When writing an enumerable, what does yield return var? [closed]?

In short, the code in the method is re-written as a state machine that does as you suspect: it keeps track of where it is in the loop, returns to the caller, and continues where it left off.

Yield is really special in C# as it doesn't follow normal flow of control. When iterating the returned IEnumerable, the BreakBytes function will be called and run until it has yielded a value. Control will then be passed back to the foreach loop.

When the loop steps to the next item, BreakBytes is resumed and run until it hits another yield. This somewhat odd construct gives the benefit that if only part of the IEnumerable is enumerated, only that part needs to be generated.

Jon Skeet can tell you all about them: csharpindepth.com/Articles/Chapter6/Iter... Yes. It keeps track of its internal state.

The yield return statement is the point where an iterator gives back an answer to the caller which is almost always a foreach loop implicity using the GetEnumerator() and MoveNext() methods of a collection that implements the IEnumerable and IEnumerator interfaces. It does indeed keep it's state. The code inside of an iterator block like that can be thought to temporarily halt after the yield return statement until the next time the MoveNext() method is called when it will pick right back up where it left off until it hits another yield return.

For a much more in-depth explanation I highly recommend the book C# in Depth, Second Edition.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions