Access to Modified Closure

前端 未结 3 1872
难免孤独
难免孤独 2020-11-22 09:01
string [] files = new string[2];
files[0] = \"ThinkFarAhead.Example.Settings.Configuration_Local.xml\";
files[1] = \"ThinkFarAhead.Example.Settings.Configuration_Glo         


        
3条回答
  •  半阙折子戏
    2020-11-22 09:05

    "files" is a captured outer variable because it has been captured by the anonymous delegate function. Its lifetime is extended by the anonymous delegate function.

    Captured outer variables When an outer variable is referenced by an anonymous function, the outer variable is said to have been captured by the anonymous function. Ordinarily, the lifetime of a local variable is limited to execution of the block or statement with which it is associated (Local variables). However, the lifetime of a captured outer variable is extended at least until the delegate or expression tree created from the anonymous function becomes eligible for garbage collection.

    Outer Variables on MSDN

    When a local variable or a value parameter is captured by an anonymous function, the local variable or parameter is no longer considered to be a fixed variable (Fixed and moveable variables), but is instead considered to be a moveable variable. Thus any unsafe code that takes the address of a captured outer variable must first use the fixed statement to fix the variable. Note that unlike an uncaptured variable, a captured local variable can be simultaneously exposed to multiple threads of execution.

提交回复
热议问题