c# - 如何创建树状结构

标签 c# data-structures

我想创建如下数据结构。 enter image description here

对于这个,我想使用键值对结构。但我无法创建它。

public class NewStructure
{
    public Dictionary<string, Dictionary<string, bool>> exportDict;
}

这是正确的方法吗?如果是这样,我如何向它插入值。如果我插入

NewStructure ns = new NewStructure();
ns.exportDict.Add("mainvar",Dictionary<"subvar",true>);

它给出了编译错误。 我什么也没想到。请有任何建议。

最佳答案

您可以通过以下方式消除错误

Dictionary<string, bool> values = new Dictionary<string, bool> ();
values.Add("subvar", true);
ns.exportDict.Add("mainvar", values);

但也许你最好尝试这样的事情:

class MyLeaf
{
  public string LeafName {get; set;}
  public bool LeafValue {get; set;}
}
class MyTree
{
  public string TreeName {get; set;}
  public List<MyLeaf> Leafs = new List<MyLeaf>();
}

然后

MyTree myTree = new MyTree();
myTree.TreeName = "mainvar";
myTree.Leafs.Add(new MyLeaf() {LeafName = "subvar", LeafValue = true});

关于c# - 如何创建树状结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11796518/

相关文章:

C# Avi 文件 : fccHandler -> codec name?

c# - 为什么 xsd.exe 会为 xs :integer? 生成字符串属性

firebase - 有没有办法在父文档不为空的情况下在 Firestore 中添加自定义路径子集合?

python - 以简单易读的方式过滤字典

java - 在不变列表中查找具有给定名称的元素

c - 使用堆栈以相反顺序打印字符串时出错

c# - 如何在 C# 中将窗体放在 Windows 任务栏上?

c# - 检查矩阵中的相邻数字

c# - Linq 选择嵌套列表的公共(public)子集

python - 树的数据结构