初始化表达式中的表达式变量Expression variables in initializers

总结Summary

我们扩展了 c # 7 中引入的功能,以允许包含表达式变量的表达式) 字段初始值设定项、属性初始值设定项、ctor 初始值设定项和查询子句中的变量声明和声明模式 (。We extend the features introduced in C# 7 to permit expressions containing expression variables (out variable declarations and declaration patterns) in field initializers, property initializers, ctor-initializers, and query clauses.

动机Motivation

由于缺乏时间,这将完成 c # 语言中剩余的几个粗糙边缘。This completes a couple of the rough edges left in the C# language due to lack of time.

详细设计Detailed design

我们删除了限制,防止表达式变量的声明 () 在 ctor 初始值设定项中的变量声明和声明模式。We remove the restriction preventing the declaration of expression variables (out variable declarations and declaration patterns) in a ctor-initializer. 此类声明的变量在构造函数的整个主体范围内。Such a declared variable is in scope throughout the body of the constructor.

我们消除了在字段或属性初始值设定项中) 变量声明和声明模式 (的表达式变量声明的限制。We remove the restriction preventing the declaration of expression variables (out variable declarations and declaration patterns) in a field or property initializer. 此类声明的变量在整个初始化表达式的范围内。Such a declared variable is in scope throughout the initializing expression.

我们消除了在转换为 lambda 主体的查询表达式子句中的表达式变量声明和声明模式) (的限制。We remove the restriction preventing the declaration of expression variables (out variable declarations and declaration patterns) in a query expression clause that is translated into the body of a lambda. 此类声明的变量在整个查询子句表达式的范围内。Such a declared variable is in scope throughout that expression of the query clause.

缺点Drawbacks

无。None.

备选方法Alternatives

在这些上下文中声明的表达式变量的适当范围并不明显,值得进一步进行 LDM 讨论。The appropriate scope for expression variables declared in these contexts is not obvious, and deserves further LDM discussion.

未解决的问题Unresolved questions

  • [] 这些变量的适当作用域是什么?[ ] What is the appropriate scope for these variables?

设计会议Design meetings

无。None.