You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: standard/conversions.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -853,7 +853,7 @@ Anonymous functions may influence overload resolution, and participate in type i
853
853
854
854
### 10.7.2 Evaluation of anonymous function conversions to delegate types
855
855
856
-
Conversionofananonymousfunctiontoadelegate type produces a delegate instance that references the anonymous function and the (possiblyempty) set of captured outer variables that are active at the time of the evaluation. When the delegate is invoked, thebodyoftheanonymousfunctionisexecuted. Thecodeinthebodyisexecutedusingthesetofcapturedoutervariablesreferencedbythedelegate. A *delegate_creation_expression* ([§12.8.17.5](expressions.md#128175-delegate-creation-expressions)) canbeusedasanalternatesyntaxforconvertingananonymousmethodtoadelegate type.
856
+
Conversionofananonymousfunctiontoadelegate type produces a delegate instance that references the anonymous function and, fornon-`static` anonymousfunctions,the (possiblyempty) setofcapturedoutervariablesthatareactiveatthetimeoftheevaluation. Whenthedelegate is invoked, thebodyoftheanonymousfunctionisexecuted. Thecodeinthebodyisexecutedusingthesetofcapturedoutervariablesreferencedbythedelegate. A *delegate_creation_expression* ([§12.8.17.5](expressions.md#128175-delegate-creation-expressions)) canbeusedasanalternatesyntaxforconvertingananonymousmethodtoadelegate type.
857
857
858
858
The invocation list of a delegate produced from an anonymous function contains a single entry. The exact target object and target method of the delegate are unspecified. In particular, itisunspecifiedwhetherthetargetobjectofthedelegateis `null`, the `this` valueoftheenclosingfunctionmember, orsomeotherobject.
If the modifier `static` is present, the anonymous function cannot capture state from the enclosing scope.
5325
+
5326
+
A non-`static` local function or non-`static` anonymous function can capture state from an enclosing `static` anonymous function, but cannot capture state outside the enclosing static anonymous function.
5327
+
5328
+
Removing the `static` modifier from an anonymous function in a valid program does not change the meaning of the program.
5329
+
5319
5330
When recognising an *anonymous_function_body* if both the *null_conditional_invocation_expression* and *expression* alternatives are applicable then the former shall be chosen.
5320
5331
5321
5332
<!-- markdownlint-disable MD028 -->
@@ -5365,11 +5376,11 @@ A *block* body of an anonymous function is always reachable ([§13.2](statements
5365
5376
>x=>x+1// Implicitly typed, expression body
5366
5377
>x=> { returnx+1; } // Implicitly typed, block body
5367
5378
> (int x) =>x+1// Explicitly typed, expression body
5368
-
> (intx) => { returnx+1; }// Explicitly typed, block body
5379
+
>static(intx) => { returnx+1; } // Explicitly typed, block body
>delegate { return1+1; } // Parameter list omitted
5374
5385
> ```
5375
5386
>
@@ -5399,8 +5410,10 @@ The body (*expression* or *block*) of an anonymous function is subject to the fo
5399
5410
-Iftheanonymousfunctionincludesasignature, theparametersspecifiedinthesignatureareavailableinthebody. Iftheanonymousfunctionhasnosignatureitcanbeconvertedtoadelegate type or expression type having parameters ([§10.7](conversions.md#107-anonymous-function-conversions)), buttheparameterscannotbeaccessedinthebody.
5400
5411
-Exceptfor by-reference parameters specified in the signature (ifany) ofthenearestenclosinganonymousfunction, itisacompile-timeerrorforthebodytoaccessaby-referenceparameter.
-Itisacompile-timeerrorforthebodytocontaina `goto` statement, a `break` statement, ora `continue` statementwhosetargetisoutsidethebodyorwithinthebodyofacontainedanonymousfunction.
5405
5418
- A `return` statementinthebodyreturnscontrolfromaninvocationofthenearestenclosinganonymousfunction, notfromtheenclosingfunctionmember.
5406
5419
@@ -5485,6 +5498,8 @@ An anonymous function cannot be a receiver, argument, or operand of a dynamicall
5485
5498
5486
5499
Any local variable, value parameter, or parameter array whose scope includes the *lambda_expression* or *anonymous_method_expression* is called an ***outer variable*** of the anonymous function. In an instance function member of a class, the this value is considered a value parameter and is an outer variable of any anonymous function contained within the function member.
5487
5500
5501
+
That said, if the modifier `static` is present, the anonymous function cannot capture state from the enclosing scope. As a result, locals, parameters, and `this` from the enclosing scope are not available to that anonymous function.
5502
+
5488
5503
#### 12.21.6.2 Captured outer variables
5489
5504
5490
5505
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 ([§9.2.9.1](variables.md#9291-general)). However, thelifetimeofacapturedoutervariableisextendedatleastuntilthedelegateorexpressiontreecreatedfromtheanonymousfunctionbecomeseligibleforgarbagecollection.
@@ -5524,7 +5539,7 @@ When an outer variable is referenced by an anonymous function, the outer variabl
5524
5539
>
5525
5540
>*endexample*
5526
5541
5527
-
Whenalocalvariableoravalueparameteriscapturedbyananonymousfunction, thelocalvariableorparameterisnolongerconsideredtobeafixedvariable ([§24.4](unsafe-code.md#244-fixed-and-moveable-variables)), butisinsteadconsideredtobeamoveablevariable. However, capturedoutervariablescannotbeusedina `fixed` statement ([§24.7](unsafe-code.md#247-the-fixed-statement)), sotheaddressofacapturedoutervariablecannotbetaken.
5542
+
Whenalocalvariableoravalueparameteriscapturedbyanon-`static`anonymousfunction, thelocalvariableorparameterisnolongerconsideredtobeafixedvariable ([§24.4](unsafe-code.md#244-fixed-and-moveable-variables)), butisinsteadconsideredtobeamoveablevariable. However, capturedoutervariablescannotbeusedina `fixed` statement ([§24.7](unsafe-code.md#247-the-fixed-statement)), sotheaddressofacapturedoutervariablecannotbetaken.
- Avariableresultingfroma *member_access* ([§12.8.7](expressions.md#1287-member-access)) oftheform `V.I`, where `V` is a fixed variable of a *struct_type*.
266
266
- A variable resulting from a *pointer_indirection_expression* ([§24.6.2](unsafe-code.md#2462-pointer-indirection)) of the form `*P`, a *pointer_member_access* ([§24.6.3](unsafe-code.md#2463-pointer-member-access)) of the form `P->I`, or a *pointer_element_access* ([§24.6.4](unsafe-code.md#2464-pointer-element-access)) of the form `P[E]`.
Copy file name to clipboardExpand all lines: standard/variables.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -71,7 +71,7 @@ For the purpose of definite-assignment checking, an array element is considered
71
71
72
72
### 9.2.5 Value parameters
73
73
74
-
A value parameter comes into existence upon invocation of the function member (method, instanceconstructor, accessor, oroperator) or anonymous function to which the parameter belongs, and is initialized with the value of the argument given in the invocation. A value parameter normally ceases to exist when execution of the function body completes. However, if the value parameter is captured by an anonymous function ([§12.21.6.2](expressions.md#122162-captured-outer-variables)), its lifetime extends at least until the delegate or expression tree created from that anonymous function is eligible for garbage collection.
74
+
A value parameter comes into existence upon invocation of the function member (method, instanceconstructor, accessor, oroperator) or anonymous function to which the parameter belongs, and is initialized with the value of the argument given in the invocation. A value parameter normally ceases to exist when execution of the function body completes. However, if the value parameter is captured by a non-`static` anonymous function ([§12.21.6.2](expressions.md#122162-captured-outer-variables)), its lifetime extends at least until the delegate or expression tree created from that anonymous function is eligible for garbage collection.
75
75
76
76
For the purpose of definite-assignment checking, a value parameter is considered initially assigned.
77
77
@@ -124,7 +124,7 @@ A ***local variable*** is declared by a *local_variable_declaration*, *declarati
124
124
125
125
A *local_variable_declaration* canoccurina *block*, a *for_statement*, a *switch_block*, ora *using_statement*. A *declaration_expression* canoccurasan `out` *argument_value*, andasa *tuple_element* thatisthetargetofadeconstructingassignment ([§12.23.2](expressions.md#12232-simple-assignment)).
0 commit comments