kogoia kogoia - 1 month ago 11
C# Question

For which reason compiler can't infer generic class constructor type?

I have generic class

public class Foo<T>
public Foo(T data) {}

And generic static method

public static class Utils
public static void Foo<T>(T data) {}

I wondering why this not work

Bar b = new Bar();


new Foo<Bar>(b);
new Foo(b); // compiler can't infer type

Answer Source

The last example won't work because there is no implicit type inference for constructors in C#.