Why is Func ambiguous with Func>?

后端 未结 2 1777
失恋的感觉
失恋的感觉 2020-12-01 06:43

This one\'s got me flummoxed, so I thought I\'d ask here in the hope that a C# guru can explain it to me.

Why does this code generate an error?

class         


        
2条回答
  •  再見小時候
    2020-12-01 07:33

    Your code requires "magic" to take place twice, once to convert from the named method group to a delegate, and once to perform overload resolution.

    Despite the fact that you have only one method named X, the compiler rules are built for the case when there are multiple.

    In addition, since delegates don't have to match the method signature exactly, the complexity is further increased. On top of that, any given method can be converted to an unlimited number of different delegate types with identical signatures.

    Your particular case looks straightforward enough, but the general case is very very hard, so the language doesn't allow it.

    If you do part of the work by hand, you'll solve the problem. e.g.

    Func d = X;
    Foo(d);
    

    should compile just fine.

提交回复
热议问题