c# - Delegate in method using variables in scope of method but outside delegate scope -


i wrote sample code have action delegate declared in method body 2 params passed , consumed delegate code without params being passed delagte. seems cleaner me explictely pass in these params delegate too, in case not, , code work fine.

i wondering how .net keeps these references available in export delegate running on new thread.

     public void mymethod(string name, complexobject myobj)     {      action export = () => {        //do name        name = name + name;        //do complex reference object         myobj.somemethod(name);     };      // more work     // example launch export on new thread     system.threading.thread newthread = new system.threading.thread(new system.threading.threadstart(export));     newthread.start();     } 

the compiler creates special type keeps variables. then, instead of storing variables on stack, instantiates instance of type every time call method. then, anonymous delegates use reference new instance access variables.


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -