That feature might be useful, but just hasn't been implemented. The C# team only has so much time, and I suppose this has just never made the cut. There are a lot of things that make this difficult: let's say you have Foo
and Foo<T>
classes, and each have non-generic and Foo<U>
constructors. If you call new Foo<int>(1)
, is that Foo..ctor<U>
or Foo<T>..ctor
? And is Foo<T>..ctor<U>
referenced by new Foo<int><string>("")
? It can't be done without introducing some new syntax, and confusing any way I can see it being done, so maybe it's best that generics are not allowed on constructors.
You could work around it by writing a generic method on your non-generic class, e.g.
void Main()
{
Foo foo = Foo.GetInstance(new BarBazAndFoobarImplementer());
}
public sealed class Foo {
private Foo(Bar value) {
this.Thing1 = value.BarProperty;
this.Thing2 = ((IBaz)value).IBazProperty;
this.Thing3 = ((IFoobar)value).IFoobarProperty;
}
public static Foo GetInstance<T>(T value) where T : Bar, IBaz, IFoobar {
return new Foo(value);
}
}
(this doesn't suffer from the above problem because you could unambiguously call Foo.GetInstance<U>(..)
and Foo<T>.GetInstance<U>(..)
)