Why is C# unable to infer the generic type argument type from a non-generic static method's signature?

后端 未结 2 2228
予麋鹿
予麋鹿 2021-02-19 22:36

I have conducted the following inference tests:

static class InferenceTest {
    static void TakeInt(int a) { }
    static int GiveInt() { return 0; }
    static         


        
2条回答
  •  -上瘾入骨i
    2021-02-19 23:18

    In general, a method name will not uniquely identify a unique type Action to which the method group could be assigned. For example, even if there's only one overload of Fred and it takes a single Cat argument, that overload could be assigned not just to an Action, but also to some other types like Action, Action, or Action. While there are some cases where one type substitution would be in every way superior to any alternative, that is not always the case. It's cleaner to define the language to require that the type of delegate be specified, than to have the compiler try to "guess", especially since having the compiler guess would mean that many things which shouldn't be breaking changes, would be (e.g. adding a method overload may render ambiguous a type inference which used to work).

    提交回复
    热议问题