As the title indicates, I would like to do the following, but the compiler won't let me:
public class MyClass
{
private List<SomeSupertype> myList;
public MyClass<T> (ICollection<T> elements) where T : SomeSupertype
{
myList = new List<SomeSuperType> ();
foreach (SomeSupertype element in elements) {
myList.Add (element);
}
}
}
Somehow, it does not seem to be possible to add a generic parameter to a constructor of a class which is itself not generic. Note that MyClass should be a non-generic type because a client should not have to distinguish between different types of MyClass once it is constructed.
Can anyone help me figure this out? Is this possible at all? It certainly is in Java :)
Edit: I forgot to mention that I am using .NET 3.5 and thus there is no support for covariant generics.
IEnumerable<SomeSupertype>?IEnumerable1` is covariant, soIEnumerable<T>is convertible toIEnumerable<SomeSupertype>wheneverT : SomeSupertype.