最近我在研究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;
}
它实际上只是用于传递数据的简单分流器。
(string, string)
结构体的文件顶部使用using NameValuePair = System.Collections.Generic.KeyValuePair<string, string>;
。虽然我发现在我的命名空间中创建一个class NameValuePair
更方便:public class NameValuePair { KeyValuePair it;
public NameValuePair( string name, string value )
{
it = new KeyValuePair( name, value );
}
public string Name { get { return it.Key; } }
public string Value { get { return it.Value; } }
}
- ToolmakerSteve