Depending on what I know when I define a method in C #, the local variables in this method will be freed from memory after the execution of the block of this method is completed [when the GC wants],
but if I have a built-in callback in the method, will these local variables be also released from memory?
In the following example, the variable [x] will retain its value after the method has completed, and the message will display the value [x] without problems, although it is in the callback !!
private void MyMethod()
{
int x = 1;
System.Timers.Timer t = new System.Timers.Timer(1000);
t.Elapsed += (sender, e) => MessageBox.Show((x++).ToString()); ;
t.Start();
}
Homam source
share