The testit () method is a closure. aString is out of scope, but testit () can still execute on it anyway. testit2 () uses a variable that did not fall out of scope (mystring), but which was also not passed to testit2 (). Is testit2 () considered closure?
string mystring = "hello world";
Action testit = new Action(delegate { string aString = "in anon method"; Debug.WriteLine(aString); });
testit();
Action testit2 = new Action(delegate { Debug.WriteLine(mystring); });
testit2();
In the second example, mystring can be updated outside the method, and these changes will be reflected in testit2 (). This does not behave like a normal method that could only capture mystring as a parameter.
source
share