kogoia kogoia - 1 year ago 68
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();


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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download