Target-Typed 条件表达式Target-Typed Conditional Expression
条件表达式转换Conditional Expression Conversion
对于条件表达式 c ? e1 : e2 ,当For a conditional expression c ? e1 : e2, when
- 和没有通用类型
e1e2,或there is no common type fore1ande2, or - ,它的通用类型为,但其中一个表达式
e1为e2,或者没有到该类型的隐式转换for which a common type exists but one of the expressionse1ore2has no implicit conversion to that type
我们定义了一个新的隐式 条件表达式转换 ,该转换允许从条件表达式到的任何类型的隐式转换 T (从到的转换,以及从到的转换) e1 T e2 T 。we define a new implicit conditional expression conversion that permits an implicit conversion from the conditional expression to any type T for which there is a conversion-from-expression from e1 to T and also from e2 to T. 如果条件表达式在和之间既没有通用类型,也不符合 e1 e2 条件表达式转换,则是错误的。It is an error if a conditional expression neither has a common type between e1 and e2 nor is subject to a conditional expression conversion.
表达式的更好转换Better Conversion from Expression
我们更改We change
表达式的更好转换Better conversion from expression
给定了
C1从表达式转换为类型的隐式转换ET1,以及C2从表达式转换为类型的隐式转换,ET2C1是比C2E不完全匹配T2且至少包含以下其中一项的更好的转换:Given an implicit conversionC1that converts from an expressionEto a typeT1, and an implicit conversionC2that converts from an expressionEto a typeT2,C1is a better conversion thanC2ifEdoes not exactly matchT2and at least one of the following holds:
E完全匹配T1(完全匹配的表达式)Eexactly matchesT1(Exactly matching Expression)T1比T2(更好的转换目标 更好的转换目标)T1is a better conversion target thanT2(Better conversion target)
toto
表达式的更好转换Better conversion from expression
给定了
C1从表达式转换为类型的隐式转换ET1,以及C2从表达式转换为类型的隐式转换,ET2C1是比C2E不完全匹配T2且至少包含以下其中一项的更好的转换:Given an implicit conversionC1that converts from an expressionEto a typeT1, and an implicit conversionC2that converts from an expressionEto a typeT2,C1is a better conversion thanC2ifEdoes not exactly matchT2and at least one of the following holds:
E完全匹配T1(完全匹配的表达式)Eexactly matchesT1(Exactly matching Expression)- **
C1不是一个 条件表达式转换 ,并且C2是一个 * 条件表达式转换 * * *。**C1is not a conditional expression conversion andC2is a *conditional expression conversion***.T1比T2(更好的转换 目标) * *,C1和C2都是 条件表达式转换 ,或者两者都不是 * 条件表达式转换 * * *,这是更好的转换目标。T1is a better conversion target thanT2(Better conversion target) **and eitherC1andC2are both conditional expression conversions or neither is a *conditional expression conversion***.
Cast 表达式Cast Expression
当前 c # 语言规范显示The current C# language specification says
窗体的 cast_expression
(T)E,其中T是一个 类型 并且E是一个 unary_expression,它执行显式转换, (将的值) 到类型的 显式 转换ET。A cast_expression of the form(T)E, whereTis a type andEis a unary_expression, performs an explicit conversion (Explicit conversions) of the value ofEto typeT.
存在 条件表达式转换 时,可能有多个从到的转换可能 E T 。In the presence of the conditional expression conversion there may be more than one possible conversion from E to T. 通过添加 条件表达式转换,我们更喜欢将任何其他转换转换为 条件表达式转换,并将 条件表达式转换 仅用作最后的手段。With the addition of conditional expression conversion, we prefer any other conversion to a conditional expression conversion, and use the conditional expression conversion only as a last resort.
设计纪要Design Notes
从表达式转换到更好的转换 的原因是处理以下情况:The reason for the change to Better conversion from expression is to handle a case such as this:
M(b ? 1 : 2);
void M(short);
void M(long);
此方法有两个小缺点。This approach does have two small downsides. 首先,它与 switch 表达式并不完全相同:First, it is not quite the same as the switch expression:
M(b ? 1 : 2); // calls M(long)
M(b switch { true => 1, false => 2 }); // calls M(short)
这仍是一项重大更改,但其作用域不太可能影响实际程序:This is still a breaking change, but its scope is less likely to affect real programs:
M(b ? 1 : 2, 1); // calls M(long, long) without this feature; ambiguous with this feature.
M(short, short);
M(long, long);
这种方法变得不明确 long ,因为对于第一个参数 (而言,转换更好,因为它不使用 条件表达式转换) ,但转换到 short 对于第二个参数而言更好 (,因为 short 是比) 更好的转换目标 long 。This becomes ambiguous because the conversion to long is better for the first argument (because it does not use the conditional expression conversion), but the conversion to short is better for the second argument (because short is a better conversion target than long). 此重大更改看起来不太严重,因为它不会在无提示的情况下更改现有程序的行为。This breaking change seems less serious because it does not silently change the behavior of an existing program.
转换表达式中的注释的原因是处理以下情况:The reason for the notes on the cast expression is to handle a case such as this:
_ = (short)(b ? 1 : 2);
此程序当前使用从到的显式 int 转换 short ,并且我们希望保留此程序当前的语言含义。This program currently uses the explicit conversion from int to short, and we want to preserve the current language meaning of this program. 此更改将在运行时 unobservable,但在以下情况下,更改将可观察:The change would be unobservable at runtime, but with the following program the change would be observable:
_ = (A)(b ? c : d);
其中 c 的类型为 C d ,其类型为 D ,并且存在从到的隐式用户定义的 C 转换 D , D A C A 以及从到的隐式用户定义的转换,以及从到的隐式用户定义的转换。where c is of type C, d is of type D, and there is an implicit user-defined conversion from C to D, and an implicit user-defined conversion from D to A, and an implicit user-defined conversion from C to A. 如果在 c # 9.0 之前编译此代码,则在为 true 时, b 我们会将从转换为 c D A 。If this code is compiled before C# 9.0, when b is true we convert from c to D then to A. 如果使用 条件表达式转换,则当 b 为 true 时,将直接从转换 c 为 A ,这将执行一系列不同的用户代码。If we use the conditional expression conversion, then when b is true we convert from c to A directly, which executes a different sequence of user code. 因此,我们将 条件表达式转换 视为转换中的最后一个手段,以保留现有行为。Therefore we treat the conditional expression conversion as a last resort in a cast, to preserve existing behavior.