Calling delegate with multiple functions having return values

前端 未结 3 1221
渐次进展
渐次进展 2021-01-04 22:18

I am trying to understand concept of delegates and have got a query. Suppose that we have a delegate defined with return type as int and accepting in 2 parameters of type in

3条回答
  •  遥遥无期
    2021-01-04 23:04

    Actually, with a little bit of trickery and casting, you can get all of the results like this:

    var b = new BinaryOp(Add);
    b += new BinaryOp(Multiply);
    
    var results = b.GetInvocationList().Select(x => (int)x.DynamicInvoke(2, 3));
    foreach (var result in results)
        Console.WriteLine(result);
    

    With output:

    5
    6
    

提交回复
热议问题