kogoia kogoia - 8 months ago 49
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#.

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