Simple method call: type arguments cannot be taken out of use

I just stumbled upon a mistake that I can’t explain.

The following code causes a compiler error in Visual Studio 2015:

class Program { private static void DoSomethingWithParameter(string s) { } private static void RegisterMethod<T>(Action<T> method) { } static void Main(string[] args) { RegisterMethod(DoSomethingWithParameter); } } 

Error:

Error CS0411 Type arguments for the method 'Program.RegisterMethod <T> (Action <T>)' cannot be taken out of use. Try explicitly specifying type arguments.

If I change the method call to

 RegisterMethod<string>(DoSomethingWithParameter); 

it works.

I think that the compiler should be able to infer the correct type from the first method call, but it is not. Can someone please explain this to me?

+5
source share

Source: https://habr.com/ru/post/1262227/


All Articles