Lambda discard parameters

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 discards (_) to be used as parameters of lambdas and anonymous methods. For example:

  • lambdas: (_, _) => 0, (int _, int _) => 0
  • anonymous methods: delegate(int _, int _) { return 0; }

Motivation

Unused parameters do not need to be named. The intent of discards is clear, i.e. they are unused/discarded.

Detailed design

Method parameters - §14.6.2 In the parameter list of a lambda or anonymous method with more than one parameter named _, such parameters are discard parameters. Note: if a single parameter is named _ then it is a regular parameter for backwards compatibility reasons.

Discard parameters do not introduce any names to any scopes. Note this implies they do not cause any _ (underscore) names to be hidden.

Simple names (§11.7.4) If K is zero and the simple_name appears within a block and if the block's (or an enclosing block's) local variable declaration space (Declarations - §7.3) contains a local variable, parameter (with the exception of discard parameters) or constant with name I, then the simple_name refers to that local variable, parameter or constant and is classified as a variable or value.

Scopes - §7.7 With the exception of discard parameters, the scope of a parameter declared in a lambda_expression (§11.16) is the anonymous_function_body of that lambda_expression With the exception of discard parameters, the scope of a parameter declared in an anonymous_method_expression (§11.16) is the block of that anonymous_method_expression.