Koşullu Ifade Target-TypedTarget-Typed Conditional Expression

Koşullu Ifade dönüştürmeConditional Expression Conversion

Koşullu bir ifade için c ? e1 : e2 ,For a conditional expression c ? e1 : e2, when

  1. ve için ortak bir tür yoktur e1 e2 veyathere is no common type for e1 and e2, or
  2. ortak bir tür var, ancak ifadelerden biri e1 veya e2 Bu türe örtük dönüştürme yokfor which a common type exists but one of the expressions e1 or e2 has no implicit conversion to that type

Koşullu ifadeden, öğesinden ve ' den ' a arasında dönüştürme yapan bir türe örtülü dönüştürmeye izin veren yeni bir örtük koşullu ifade dönüştürmesi tanımladık 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. Koşullu bir ifadenin, ve arasında ortak bir tür yoksa e1 ve e2 koşullu ifade dönüştürmeye tabi olması hatadır.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.

Deyimden daha iyi dönüştürmeBetter Conversion from Expression

DeğiştiririzWe change

Deyimden daha iyi dönüştürmeBetter conversion from expression

C1Bir ifadeden bir türe dönüştüren örtük bir dönüştürme E T1 ve bir ifadeden bir türe dönüştüren örtük bir dönüştürme C2 E T2 C1 belirtildiğinde, tam olarak eşleşmeyen C2 E T2 ve en az bir tane olan bir dönüştürme daha iyi bir dönüştürmedir:Given an implicit conversion C1 that converts from an expression E to a type T1, and an implicit conversion C2 that converts from an expression E to a type T2, C1 is a better conversion than C2 if E does not exactly match T2 and at least one of the following holds:

kullanıcısıto

Deyimden daha iyi dönüştürmeBetter conversion from expression

C1Bir ifadeden bir türe dönüştüren örtük bir dönüştürme E T1 ve bir ifadeden bir türe dönüştüren örtük bir dönüştürme C2 E T2 C1 belirtildiğinde, tam olarak eşleşmeyen C2 E T2 ve en az bir tane olan bir dönüştürme daha iyi bir dönüştürmedir:Given an implicit conversion C1 that converts from an expression E to a type T1, and an implicit conversion C2 that converts from an expression E to a type T2, C1 is a better conversion than C2 if E does not exactly match T2 and at least one of the following holds:

  • E tam olarak eşleşir T1 (tam olarak eşleşen ifade)E exactly matches T1 (Exactly matching Expression)
  • **C1 , koşullu ifade dönüştürmesi değildir ve C2 bir * koşullu ifade dönüştürmedir * * *.**C1 is not a conditional expression conversion and C2 is a *conditional expression conversion***.
  • T1 daha iyi bir dönüştürme hedefi T2 (daha iyi dönüştürme hedefi) * * ve C1 C2 her ikisi de koşullu ifade dönüştürmelerdir veya ikisi de bir * koşullu ifade dönüştürmesi * * * değildir.T1 is a better conversion target than T2 (Better conversion target) **and either C1 and C2 are both conditional expression conversions or neither is a *conditional expression conversion***.

Atama IfadesiCast Expression

Geçerli C# dil belirtimi şöyle diyorThe current C# language specification says

Bir (T)E T tür olan ve bir unary_expression olan cast_expression formun bir türü, bir E Açık dönüştürme (Açık dönüştürmeler) gerçekleştirir E T .A cast_expression of the form (T)E, where T is a type and E is a unary_expression, performs an explicit conversion (Explicit conversions) of the value of E to type T.

Koşullu ifade dönüştürmesinin varlığı ' nde, ' den çok olası bir dönüştürme olabilir E T .In the presence of the conditional expression conversion there may be more than one possible conversion from E to T. Koşullu ifade dönüştürmesinin eklenmesiyle, koşullu ifade dönüştürmeye yönelik diğer dönüştürmeleri tercih ediyoruz ve koşullu ifade dönüştürmeyi yalnızca son çare olarak kullanacaksınız.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.

Tasarım NotlarıDesign Notes

Deyimden daha iyi dönüştürme için değişikliğin nedeni, şöyle bir durum işleymedir: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);

Bu yaklaşımın iki küçük kenarı vardır.This approach does have two small downsides. İlk olarak, anahtar ifadesiyle tam olarak aynı değildir: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)

Bu hala önemli bir değişiklik olmakla kalmaz, kapsamı gerçek programları etkilememe olasılığı düşüktür: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);

Bu, dönüşümü long ilk bağımsız değişken için daha iyi olduğundan ( koşullu ifade dönüştürmeyi kullanmadığından) belirsiz hale gelir, ancak dönüştürme short ikinci bağımsız değişken için daha iyidir (çünkü short öğesinden daha iyi bir dönüştürme hedefi olduğundan 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). Bu son değişiklik, var olan bir programın davranışını sessizce değiştirmediğinden daha az önemlidir.This breaking change seems less serious because it does not silently change the behavior of an existing program.

Atama ifadesindeki notların nedeni, şöyle bir durum işleymelidir:The reason for the notes on the cast expression is to handle a case such as this:

_ = (short)(b ? 1 : 2);

Bu program şu anda öğesinden açık dönüştürmeyi kullanıyor int short ve bu programın geçerli dil anlamını korumak istiyoruz.This program currently uses the explicit conversion from int to short, and we want to preserve the current language meaning of this program. Değişiklik, çalışma zamanında unobservable olabilir, ancak aşağıdaki programla değişiklik observable olur:The change would be unobservable at runtime, but with the following program the change would be observable:

_ = (A)(b ? c : d);

nerede tür, türüdür ve ' den örtük bir Kullanıcı tanımlı dönüştürme ve ' den ' a örtük bir Kullanıcı tanımlı dönüştürme c C d D vardır 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. Bu kod C# 9,0 ' den önce derlenmişse, true olduğunda, ' b dan ' a dönüştürüyoruz 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. Koşullu ifade dönüştürmeyi kullandığımızda, doğru olduğunda, b c A farklı bir Kullanıcı kodu sırası yürüten, ' den doğrudan öğesine dönüştürüyoruz.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. Bu nedenle, var olan davranışı korumak için koşullu ifade dönüştürmeyi bir dönüştürmede son çare olarak değerlendiyoruz.Therefore we treat the conditional expression conversion as a last resort in a cast, to preserve existing behavior.