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
- ve için ortak bir tür yoktur
e1e2veyathere is no common type fore1ande2, or - ortak bir tür var, ancak ifadelerden biri
e1veyae2Bu türe örtük dönüştürme yokfor which a common type exists but one of the expressionse1ore2has 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ürmeET1ve bir ifadeden bir türe dönüştüren örtük bir dönüştürmeC2ET2C1belirtildiğinde, tam olarak eşleşmeyenC2ET2ve en az bir tane olan bir dönüştürme daha iyi bir dönüştürmedir: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:
Etam olarak eşleşirT1(tam olarak eşleşen ifade)Eexactly matchesT1(Exactly matching Expression)T1daha iyi bir dönüştürme hedefiT2(daha iyi dönüştürme hedefi)T1is a better conversion target thanT2(Better conversion target)
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ürmeET1ve bir ifadeden bir türe dönüştüren örtük bir dönüştürmeC2ET2C1belirtildiğinde, tam olarak eşleşmeyenC2ET2ve en az bir tane olan bir dönüştürme daha iyi bir dönüştürmedir: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:
Etam olarak eşleşirT1(tam olarak eşleşen ifade)Eexactly matchesT1(Exactly matching Expression)- **
C1, koşullu ifade dönüştürmesi değildir veC2bir * koşullu ifade dönüştürmedir * * *.**C1is not a conditional expression conversion andC2is a *conditional expression conversion***.T1daha iyi bir dönüştürme hedefiT2(daha iyi dönüştürme hedefi) * * veC1C2her ikisi de koşullu ifade dönüştürmelerdir veya ikisi de bir * koşullu ifade dönüştürmesi * * * değildir.T1is a better conversion target thanT2(Better conversion target) **and eitherC1andC2are 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)ETtür olan ve bir unary_expression olan cast_expression formun bir türü, birEAçık dönüştürme (Açık dönüştürmeler) gerçekleştirirET.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.
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.