Redaguoti

Bendrinti naudojant


Static anonymous functions

Note

This article is a feature specification. The specification serves as the design document for the feature. It includes proposed specification changes, along with information needed during the design and development of the feature. These articles are published until the proposed spec changes are finalized and incorporated in the current ECMA specification.

There may be some discrepancies between the feature specification and the completed implementation. Those differences are captured in the pertinent language design meeting (LDM) notes.

You can learn more about the process for adopting feature speclets into the C# language standard in the article on the specifications.

Summary

Allow a 'static' modifier on lambdas and anonymous methods, which disallows capture of locals or instance state from containing scopes.

Motivation

Avoid unintentionally capturing state from the enclosing context, which can result in unexpected retention of captured objects or unexpected additional allocations.

Detailed design

A lambda or anonymous method may have a static modifier. The static modifier indicates that the lambda or anonymous method is a static anonymous function.

A static anonymous function cannot capture state from the enclosing scope. As a result, locals, parameters, and this from the enclosing scope are not available within a static anonymous function.

A static anonymous function cannot reference instance members from an implicit or explicit this or base reference.

A static anonymous function may reference static members from the enclosing scope.

A static anonymous function may reference constant definitions from the enclosing scope.

nameof() in a static anonymous function may reference locals, parameters, or this or base from the enclosing scope.

Accessibility rules for private members in the enclosing scope are the same for static and non-static anonymous functions.

No guarantee is made as to whether a static anonymous function definition is emitted as a static method in metadata. This is left up to the compiler implementation to optimize.

A non-static local function or anonymous function can capture state from an enclosing static anonymous function but cannot capture state outside the enclosing static anonymous function.

Removing the static modifier from an anonymous function in a valid program does not change the meaning of the program.