views:

62

answers:

1

I have this code for example

public class BaseClass
{}

public class DerivedClass1 : BaseClass
{}

public class DerivedClass2 : BaseClass
{ }

public class DerivedClass3 : BaseClass
{ }

public class GenericBaseClass<T> where T : BaseClass
{
    public List<T> collection { get; set; }
}

Now I'd like to create a new class which would inherit from GenericBaseClass<T> and have somehow a collection List in this class.

Is this even possible this way ? Because List<BaseClass> = new List<DerivedCLass3>(); isn't valid.

Or the only way to achieve this is to remove the collection property from the GenericBaseClass and add a collection List directly in the derived class.

I hope I expressed myself understandable as English isn't my first language :-)

+2  A: 
public class GenericBaseClass<T> where T : BaseClass
{
    public List<T> collection { get; set; }
}

public class GenericDerivedClass1 : GenericBaseClass<DerivedClass1>
{
    // Here the collection property will be of type List<DerivedClass1>
}
Darin Dimitrov
I use this GenericBaseClass<T> as a parameter type in one method and I'm unable to pass GenericDerivedClass1 as the parameter value. Is there any way to do it ?