Lambda 弃元参数Lambda discard parameters
总结Summary
允许丢弃 (用作 _
lambda 和匿名方法的参数) 。Allow discards (_
) to be used as parameters of lambdas and anonymous methods.
例如:For example:
- lambda:
(_, _) => 0
,(int _, int _) => 0
lambdas:(_, _) => 0
,(int _, int _) => 0
- 匿名方法:
delegate(int _, int _) { return 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
方法参数 在使用多个名为的参数的 lambda 或匿名方法的参数列表中 _
,此类参数是丢弃参数。Method parameters 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.
简单名称 如果 K
为零,并且 simple_name 出现在 块 中,并且如果 块 的 (或封闭 块 的) 局部变量声明空间 (声明 中) 包含局部变量,则参数 (,但对于丢弃参数) 或具有 name 的常量,则 I
simple_name 引用该本地变量、参数或常量,并归类为变量或值。Simple names 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) 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.
作用域除丢弃参数外,在 (lambda_expression 中声明的参数的作用域) anonymous_function_body 是除放弃 参数外, lambda_expression 中声明的参数的 范围, anonymous_method_expression ( 匿名函数表达式) 是该 anonymous_method_expression 的 块。Scopes With the exception of discard parameters, the scope of a parameter declared in a lambda_expression (Anonymous function expressions) 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 (Anonymous function expressions) is the block of that anonymous_method_expression.