Closing over the Loop Variable in C#

后端 未结 1 641
旧巷少年郎
旧巷少年郎 2020-12-10 07:37

From this post, I was told that the following section of code suffered from \"the egregious act of closing over the loop variable.\"

    foreach (Canidate ca         


        
相关标签:
1条回答
  • 2020-12-10 08:24

    Closing over variables that later change values is what is causing the problem.

    • candidate changes every "lap" around the loop and will cause problems.
    • can is newly created every "lap" around the loop, never changes, and won't cause problems.
    0 讨论(0)
提交回复
热议问题