c # in the yield lexical

The role of yield keyword is the current elements of the collection to return immediately, examples:

Breakpoints can be seen, the console displays a set of each element will process the query to fetch the collection elements.

In fact, yield return is "syntactic sugar", its essence is to generate a query of the class that implements the generic IEnumerable and IEnumerator, non-generic interface, so it can be iterative (looping through).

1. The return value must be IEnumerable, IEnumerable <T>, IEnumerator, IEnumerator <T>

2. anonymous method can not be used yield, method parameters can not be used ref, in or out

yield break Usage: termination acquiring the collection element

 

Guess you like

Origin www.cnblogs.com/1016391912pm/p/11978698.html