How does a lambda in C# bind to the enumerator in a foreach?

后端 未结 5 1820
囚心锁ツ
囚心锁ツ 2020-12-09 18:27

I just came across the most unexpected behavior. I\'m sure there is a good reason it works this way. Can someone help explain this?

Consider this code:



        
5条回答
  •  暖寄归人
    2020-12-09 19:10

    See Eric Lippert's blog post on this issue; it has to do with how iterator variables are scoped in code, and how that applies to lambda closures and hoisted functions.

提交回复
热议问题