2012-01-17 11 views
6

C#のNULL合体演算子(??)に関する次の理解をもとにしてください。Null-Coalescing演算子の結果の暗黙のキャスト

int? input = -10; 
int result = input ?? 10;//Case - I 
//is same as: 
int result = input == null? input : 10; // Case - II 

ケースIとケースIIは同じ意味で使用されますが、

Case-Iコンパイラがintを暗黙的にキャストできることは驚くべきことですか? intに変換すると、Case-IIでエラーが表示されます。 'エラー1暗黙的に' int型を変換できませんか? 'ご関心のため

感謝。

答えて

5

?それは私がnullで合体演算子については行方不明だということです何

「「INT」に三項演算子と第二ケースの作業を行うには、あなたが使用することができます次

int result = input != null ? input.Value : 10; 
Nullable<T>

タイプのValueプロパティは(この場合、intT値を返し

別のオプションNullable<T>.HasValueを使用することです:

int result = input.HasValue ? input.Value : 10; 

myNullableInt != null構築物は上記HasValueコールのための唯一のシンタックスシュガーです。

+0

int? NULL_Int = 1; int NORM_Int = 2; NULL_Int = NORM_Int; // OK NORM_Int = NULL_Int; // NO, you can't assign a null to an int 
最初のテストは '使用すべき='なく== ''より – hvd

+0

@hvd:絶対に正しいが、私は悪い例を有するためOPのせい。 : - P編集されました。 –

+0

実際、私はあなたの答えは少し外れていると思います。 b:c'( 'b'は' int''、 'c'は' int'です)は完全に有効です。しかし、結果には 'int? '型があるので、'(a?b:c).Value'が必要です。またはあなたの提案された訂正。 – hvd

5

null-coalescing演算子??のこの動作は、文書化された言語機能です。詳しくは、C#4.0言語仕様のセクション7.13を参照してください。

The type of the expression a ?? b depends on which implicit conversions are available on the operands. In order of preference, the type of a ?? b is A0, A, or B, where A is the type of a (provided that a has a type), B is the type of b (provided that b has a type), and A0 is the underlying type of A if A is a nullable type, or A otherwise. Specifically, a ?? b is processed as follows:

  • If A exists and is not a nullable type or a reference type, a compile-time error occurs.

  • If b is a dynamic expression, the result type is dynamic. At run-time, a is first evaluated. If a is not null, a is converted to dynamic, and this becomes the result. Otherwise, b is evaluated, and this becomes the result.

  • Otherwise, if A exists and is a nullable type and an implicit conversion exists from b to A0, the result type is A0. At run-time, a is first evaluated. If a is not null, a is unwrapped to type A0, and this becomes the result. Otherwise, b is evaluated and converted to type A0, and this becomes the result.

  • Otherwise, if A exists and an implicit conversion exists from b to A, the result type is A. At run-time, a is first evaluated. If a is not null, a becomes the result. Otherwise, b is evaluated and converted to type A, and this becomes the result.

  • Otherwise, if b has a type B and an implicit conversion exists from a to B, the result type is B. At run-time, a is first evaluated. If a is not null, a is unwrapped to type A0 (if A exists and is nullable) and converted to type B, and this becomes the result. Otherwise, b is evaluated and becomes the result.

  • Otherwise, a and b are incompatible, and a compile-time error occurs.

条件演算子a ? b : cが異なる動作をする理由については、セクション7.14を参照してください。

Download the specificationあなたのレジャーの両方で完全に読んでください。三項演算子で使用中の両方のタイプが正確に同じでなければなりませんので、これは(コンパイルされません今

int result = input != null ? input : 10; 

あなたはおそらくを意味 -

0
int result = input == null ? input : 10; 

あなたはあなたの状態が第二の場合において混合ましたそしてint?は)intと同じではありません - あなたが解決策として、単純なキャストを使用することができます。

int result = input != null ? (int)input : 10; 
-1

より簡潔explantion:

関連する問題