I feel like I should know exactly how this works since I use it regularly and it seems to work, but I don't. As an example, how would this behave:
void MyMethod()
{
for(int i = 0; i < 100; i++)
{
DoSomeWork(() =>
{
Console.WriteLine(i);
});
}
}
How does the scope of i
change? Is the value copies over to the callback or does the callback function have access to the variable when it is called? Will the variable thus be in memory after MyMethod
has long been finished?
Is there a good explanation for these kinds of things, especially when using lambda and async
methods?