所以最近我一直在研究 C#,所有的泛型集合让我有点困惑。假设我想表示一个数据结构,其中树的头部是一个键值对,然后在它下面有一个可选的键值对列表(但级别不超过这些)。这样合适吗?
public class TokenTree
{
public TokenTree()
{
/* I must admit to not fully understanding this,
* I got it from msdn. As far as I can tell, IDictionary is an
* interface, and Dictionary is the default implementation of
* that interface, right?
*/
SubPairs = new Dictionary<string, string>();
}
public string Key;
public string Value;
public IDictionary<string, string> SubPairs;
}
它实际上只是一个简单的分流器,用于传递数据。
最佳答案
有一个实际的数据类型叫做KeyValuePair,像这样使用
KeyValuePair<string, string> myKeyValuePair = new KeyValuePair<string,string>("defaultkey", "defaultvalue");
关于c# - 键值对数据结构的最佳实现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8800/