type argument from Action cannot be inferred, but from Func can be

后端 未结 1 1554
感情败类
感情败类 2021-01-11 22:27

I\'ve been playing a little with generics and delegates and I have found something I don\'t understand. I have quite similar generic static methods, one accepts Action

相关标签:
1条回答
  • 2021-01-11 22:45

    Methods can be overloaded by their arguments and all overloads form one method group, so for example void Xyz(int i) and void Xyz(string s) are within same method group called Xyz. Compiler is not able to deduct a type of argument even if user defines only one method, because behaviour of compiler is quite strict.

    Methods can't be overloaded by return types, so you can't have int Xyz() and string Xyz() within same class. Return type can be deducted by compiler easily, because there is no overloading.

    It was not obvious for me for the first time, but it has been quite clear after I realized that I could create an overload.

    0 讨论(0)
提交回复
热议问题