i have following code:
class foo<ktype, vtype> { public ktype key; public vtype value; public list<foo<ktype, vtype>> children; } class test { public test() { var x = new list<foo<int, string>>() { new foo<int, string>() {key = 1, value = "a", children = new list<foo<int, string>>() { new foo<int, string>() {key = 1, value = "a"}, new foo<int, string>() {key = 2, value = "b"} } }, new foo<int, string>() {key = 2, value = "b"} }; } }
it works beautifully in allowing me have tree of nested "pairs" of {ktype, vtype}. however, because have list , not dictionary, don't have way of enforcing keys unique. how can construct tree or chain of dictionaries or equivalent? i'd change underlying type of "children" dictionary, takes keyvaluepair, takes 2 items, key , value, , there'd no room grandchildren.
as mentioned @jdweng, dictionary map keys foos:
class foo<ktype, vtype> { public vtype value; public dictionary<ktype, foo<ktype, vtype>> children; } class test { public test() { var root = new foo<int, string> { value = "root", children = new dictionary<int, foo<int, string>> { { 1, new foo<int, string> { value = "a", children = new dictionary<int, foo<int, string>> { {1, new foo<int, string> {value = "a", children = null}}, {2, new foo<int, string> {value = "b", children = null}} } } }, { 2, new foo<int, string> { value = "b", children = null } } } }; } }