Jakub Čermoch Jakub Čermoch - 1 year ago 112
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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download