I have a base class that inherits List<> which has multiple classes derived from it.
I want to create a method that can add items to any class that has inherited from this base class, is this possible ?
pseudo code:
public class BaseList<T> : List<T> where T: baseItem { } public BaseList<T> AddItems<T>(int someArg) where T: new(), baseItem { BaseList<T> temp = new BaseList<T>(); temp.add(new T()); temp.add(new T()); return temp; } public class MyDerived: BaseList<SomeClass> { }
Then to call this code:
MyDerived temp = (MyDerived)AddItems();
is something like this possible ? I can’t figure out the correct syntax
Do you really need to derive from
List<T>instead of using composition?It sounds like you want a static method in a non-generic type:
It doesn’t seem particularly nice though… perhaps if you explained the purpose of this, we could come up with a better idea.