2015-09-22 11 views
6

This answer私の別の質問にはコンパイルされませんでしたが、表面上ではそうすべきです(これは同じ質問ではありません。コンパイラがnull合体演算子とトラブルに実行されているのはなぜヌル合体とラムダ

はなく、構文・無糖の場合/他のバージョンと、

private Func<MyT, bool> SegmentFilter { get; set; } 

public MyConstructor(Func<MyT, bool> segmentFilter = null) 
{ 
    // This does not compile 
    // Type or namespace mas could not be found 
    SegmentFilter = segmentFilter ?? (mas) => { return true; }; 

    // This (equivalent?) form compiles just fine 
    if (segmentFilter == null) 
    { 
     SegmentFilter = (mas) => { return true; }; 
    } 
    else 
    { 
     SegmentFilter = segmentFilter; 
    } 
} 

を考えると?

答えて

9

??=>よりも優先度が高いためです。ラムダを()にラップして簡単に修正できます。

SegmentFilter = segmentFilter ?? ((mas) => { return true; }); 
+0

ありがとうございます。 「その他」の回答は、同様にあなたのものでした。私はそれをコンパイルするために、それを編集した;-) –