Jakub Čermoch Jakub Čermoch - 11 days ago 6
C# Question

Generic to self

I wanna make generic tree.

I have base class like:

public class TreeItemModelBase<T>
{
protected List<T> children;
...
}


and derived classes like:

public class StorageTreeItemModel : TreeItemModelBase<StorageTreeItemModel>
{
...
}


Is this OK? I am not sure if I can use generic like this (Use StorageTreeItemModel like T).

Thank you

Answer

Yes, inheriting a generic class or implementing a generic interface using your own class as a generic type parameter is fine.

Implementations that avoid this are available and personally I think they are easier to understand, but as long as you are happy with the code, from a technical side there is nothing wrong with it.