Why doesn’t C# support implied generic types on class constructors?

Actually, your question isn’t bad. I’ve been toying with a generic programming language for last few years and although I’ve never come around to actually develop it (and probably never will), I’ve thought a lot about generic type inference and one of my top priorities has always been to allow the construction of classes without having to specify the generic type.

C# simply lacks the set of rules to make this possible. I think the developers never saw the neccesity to include this. Actually, the following code would be very near to your proposition and solve the problem. All C# needs is an added syntax support.

class Foo<T> {
    public Foo(T x) { … }
}

// Notice: non-generic class overload. Possible in C#!
class Foo {
    public static Foo<T> ctor<T>(T x) { return new Foo<T>(x); }
}

var x = Foo.ctor(42);

Since this code actually works, we’ve shown that the problem is not one of semantics but simply one of lacking support. I guess I have to take back my previous posting. 😉

Leave a Comment