2017-06-21 2 views
2

私は他の誰かのユーティリティメソッドを使用しています。読み込みに問題があるラムダ式構文を使用しているようです。引数がのFuncのプロトタイプは、例えばコードでラムダ関数の本体はカッコで囲まれていますか?

delegate TResult System.Func<in T1, in T2, out TResult>(T1 arg1, T2 arg2) 

あると

Func<Action<int>, Action<Exception>, EventHandler<CustomEventArgs>> getCompleteHandler 

定義される、方法を使って呼び出される:コールの第二ラインで

getCompleteHandler: (Action<int> complete, Action<Exception> reject) => 
         (
         (object sender, CustomEventArgs args) => 
         { 
          complete(args.IntVar); 
         } 
        ) 

、括弧内側のラムダを囲む第1ラムダ演算子に直接追従する。私は他の場所でこの構文を見たことがありませんが、ラムダ式全体があり、型キャストとメソッド本体以外は何も期待していません。内側のラムダがgetCompleteHandlerを表すのは何ですか?

答えて

0

x = 1; x = (1); x = ((1)); x = ..

(グループ化)括弧は事実上無視されます。コーダーは、(彼/彼女に)明瞭さを加えたり、何らかの慣習に従ったりするため、シンタックスを選択している可能性があります。このかっこを使用しても、構文解析やセマンティクスはまったく変更されません。

個人的に、私はかっこを追加しませんでした。


評価外側ラムダとして-funcがcomplete上にクロージャを形成する、内側ラムダなどのアクションを返します。返されたActionは後で評価されます。

想像:

// -> Func<Action<int>, Action<Exception>,  // arguments 
//   Action<object, CustomEventArgs>>  // return value 
var getCompleteHandler = /* that */ 

// create the completion handler by invoking the "get" function 
// -> Action<object, CustomEventArgs>   // same as return value 
var completionHandler = getCompletionHandler(
    (x) => Console.WriteLine(x), 
    (ex) => Console.WriteLine("Exception!: " + ex)); 

// call the completion handler any number of times.. 
foreach (var i in Enumerable.Range(42)) { 
    completionHandler(this, new CustomEventArgs { IntVar = i }); 
}