Write some beautiful code like this:
1: IEnumerable<string> citiesList = cities.SelectMany(pair =>
2: {
3: yield return pair.MainCity;
4: yield return pair.SecondaryCity;
5: });
invoke the compiler from the command line (yes, sometimes I like it "old school") and let this error message wake you up from your aesthetic dreams...
The yield statement cannot be used inside an anonymous method or lambda expression
Do some googling and find this post from Eric Lippert explaining it. Worth reading material.
What I conclude from this is:
We sometimes forget all the logic behind the compiler magic. Anonymous methods mean the compiler has to create named methods in the background, or classes when these Anonymous methods grab variables from its lexical scope (closures). Iterator blocks mean the compiler has to create a nested class implementing IEnumerable and IEnumerator (well, more or less, if the iterator block is in the GetEnumerator method of an IEnumerable class, the nested class that gets created only needs to implement IEnumerator).
There is a sequence of steps to follow, first the compiler transforms all the Anonymous Methods, then the Iterator Blocks. When an Anonymous method contains an Iterator block we have a problem with that sequence...
You can check some code here
and to spice up this post, a Reflector screenshot showing some of the compiler generated code:
No comments:
Post a Comment