Terminology when copying a captured variable for lambda / anion method

I translated this code (it has a bad side effect that it just captures an external variable):

foreach (TaskPluginInfo tpi in Values)
{                    
    GenerateMenu(menuStrip, tpi.MenuTree, tpi.MenuText, delegate { tpi.ShowTask() });
}

To this code (because above does not work):

foreach (TaskPluginInfo tpi in Values)
{                    
    // must capture the variable
    var x = tpi;
    GenerateMenu(menuStrip, tpi.MenuTree, tpi.MenuText, delegate { x.ShowTask(); });
}

What is the correct terminology for this work around this little-known side effect? At the moment, I commented that "must capture the variable." Is capturing the right terminology?

+3
source share
6 answers

, tpi, x ( ), ... , ( x) .

, ; " ,

+2

, . , .

, ? , "" - . , , , , : -)

+1

Resharper " ", - " ", : " , ".

+1
0

, ; "". :

, GenerateMenu, x.

, GenerateMenu, , x.

google " " " ".

0

"assign the iteration variable to a local variable that is inside the loop"

0
source

Source: https://habr.com/ru/post/1711901/


All Articles