Search code examples
c#genericstreeviewstrong-typing

How to create a complete generic TreeView like data structure


I want to create a completely generic treeview like structure. some thing like this:

public class TreeView<T, K, L>
{
    public T source;
    public K parent;
    public List<L> children;
}

as you can see in this class source, parent and also the children, all have a different generic data type. also i want my tree view to have unlimited number of levels (not just 3). this way when i want to work with my nodes in the code, all of them are going to be strongly typed. not just objects that i need to convert them to their original type.

is it possible to create this kind of structure in c#, a treeview which all of its nodes are strongly typed?

thanks


Solution

  • This is the big problem with tree data structures. It's easy to define homogeneous trees, but in the real world, trees are often composed of heterogeneous object types. A good example of this is a file system, where the tree contains drives, folders, and files.

    You can only create a type safe tree if you know exactly the shape of the tree at compile time. Of course, this eliminates every real world use case for trees.