私はVS 2010から2015にアップグレードしました。私は新しいnull-conditional operatorが好きです。これはヌル伝播としても知られています。これは、例えば、あなたのコードを簡素化することができます:ヌル条件演算子はboolではなくboolに評価されますか?期待通りに
string firstCustomerName = customers?[0].Name; // null if customers or the first customer is null
別の1:
Enumerable.Count
が有効数と任意の
nulls
前に区別するために
int
を返す場合でも
Nullable<int>
を返し
int? count = customers?[0]?.Orders?.Count(); // null if customers, the first customer, or Orders is null
。それは非常に直感的で非常に便利です。
しかし、なぜ期待通り(それはfalse
を返します)このコンパイル作業を行います。
string text = null;
bool contains = text?.IndexOf("Foo", StringComparison.CurrentCultureIgnoreCase) >= 0;
これは、コンパイル(それはない)bool?
を返すかべきではないのいずれか。あなたが実際に持っている何
おっとを、私はあなたができる、私はそれを自分自身を持っていると思いますそれを書き直してください: 'int? indexof =テキスト?.IndexOf( "Foo"、StringComparison.CurrentCultureIgnoreCase); bool contains = indexof> = 0; ' –
私は'?> = '演算子に投票します。:) –
@DStanley:他の[オペレータカテゴリ](https://msdn.microsoft.com/en-us/library/aa691323(v = vs.71).aspx)を含むものもあります。良いよりも害を及ぼすでしょう。 –