kogoia kogoia - 1 month ago 12
C# Question

Why generic class constructor requires type argument?

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();

Utils.Foo<Bar>(b);
Utils.Foo(b);

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#.