I'm trying to build a generic class whose constructor introduces an additional type, but the compiler says no-no.
I don't quite understand why the following doesn't work:
public class Foo<T>
{
public Foo<T,TBar>(TBar tBar)
{
...
}
}
It's not critical as I can write the class using a fluent api (which might be preferred), but I'd still like to understand why I can't. The only explanation I can think of is that the compiler doesn't like method-level generic type declaration mixed with class-level generic type declaration.
No. Only types and methods can be generic. You can't have generic properties, events, fields, constructors or finalizers.
Note that this has nothing to do with whether the containing type is generic or not - it's just a limitation of the language (and CLR, I suspect), basically. (Interestingly, Java does allow generic constructors - but generics in Java are pretty different to those in C# anyway.)
See more on this question at Stackoverflow