<p>So I've been poking around with C# a bit lately, and all the Generic Collections have me a little confused. Say I wanted to represent a data structure where the head of a tree was a key value pair, and then there is one optional list of key value pairs below that (but no more levels than these). Would this be suitable?</p>

<pre><code>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&lt;string, string&gt;();
    }

    public string Key;
    public string Value;
    public IDictionary&lt;string, string&gt; SubPairs;
}
</code></pre>

<p>It's only really a simple shunt for passing around data.</p>

[{'answer_id': 8811, 'author': 'Adam Haile', 'author_id': 194, 'author_profile': 'https://Stackoverflow.com/users/194', 'pm_score': 8, 'selected': True, 'text': '<p>There is an actual Data Type called KeyValuePair, use like this</p>\n\n<pre><code>KeyValuePair&lt;string, string&gt; myKeyValuePair = new KeyValuePair&lt;string,string&gt;("defaultkey", "defaultvalue");\n</code></pre>\n'}, {'answer_id': 8813, 'author': 'Lasse V. Karlsen', 'author_id': 267, 'author_profile': 'https://Stackoverflow.com/users/267', 'pm_score': 1, 'selected': False, 'text': '<p><a href="http://msdn.microsoft.com/en-us/library/xfhwa508.aspx" rel="nofollow noreferrer">Dictionary Class</a> is exactly what you want, correct.</p>\n\n<p>You can declare the field directly as Dictionary, instead of IDictionary, but that\'s up to you.</p>\n'}, {'answer_id': 8814, 'author': 'Coincoin', 'author_id': 42, 'author_profile': 'https://Stackoverflow.com/users/42', 'pm_score': 3, 'selected': False, 'text': '<p>There is a KeyValuePair built-in type. As a matter of fact, this is what the IDictionary is giving you access to when you iterate in it.</p>\n\n<p>Also, this structure is hardly a tree, finding a more representative name might be a good exercise.</p>\n'}, {'answer_id': 8816, 'author': 'Jon Limjap', 'author_id': 372, 'author_profile': 'https://Stackoverflow.com/users/372', 'pm_score': 4, 'selected': False, 'text': '<p>One possible thing you could do is use the Dictionary object straight out of the box and then just extend it with your own modifications:</p>\n\n<pre><code>public class TokenTree : Dictionary&lt;string, string&gt;\n{\n    public IDictionary&lt;string, string&gt; SubPairs;\n}\n</code></pre>\n\n<p>This gives you the advantage of not having to enforce the rules of IDictionary for your Key (e.g., key uniqueness, etc).</p>\n\n<p>And yup you got the concept of the constructor right :)</p>\n'}, {'answer_id': 8818, 'author': 'Lasse V. Karlsen', 'author_id': 267, 'author_profile': 'https://Stackoverflow.com/users/267', 'pm_score': 2, 'selected': False, 'text': '<p>@<a href="https://stackoverflow.com/users/733/jay-mooney">Jay Mooney</a>: A generic Dictionary class in .NET is actually a hash table, just with fixed types.</p>\n\n<p>The code you\'ve shown shouldn\'t convince anyone to use Hashtable instead of Dictionary, since both code pieces can be used for both types.</p>\n\n<p>For hashtable:</p>\n\n<pre><code>foreach(object key in h.keys)\n{\n     string keyAsString = key.ToString(); // btw, this is unnecessary\n     string valAsString = h[key].ToString();\n\n     System.Diagnostics.Debug.WriteLine(keyAsString + " " + valAsString);\n}\n</code></pre>\n\n<p>For dictionary:</p>\n\n<pre><code>foreach(string key in d.keys)\n{\n     string valAsString = d[key].ToString();\n\n     System.Diagnostics.Debug.WriteLine(key + " " + valAsString);\n}\n</code></pre>\n\n<p>And just the same for the other one with KeyValuePair, just use the non-generic version for Hashtable, and the generic version for Dictionary.</p>\n\n<p>So it\'s just as easy both ways, but Hashtable uses Object for both key and value, which means you will box all value types, and you don\'t have type safety, and Dictionary uses generic types and is thus better.</p>\n'}, {'answer_id': 8826, 'author': 'kokos', 'author_id': 1065, 'author_profile': 'https://Stackoverflow.com/users/1065', 'pm_score': 1, 'selected': False, 'text': "<p>Use something like this:  </p>\n\n<pre><code>class Tree &lt; T &gt; : Dictionary &lt; T, IList&lt; Tree &lt; T &gt; &gt; &gt;  \n{  \n}  \n</code></pre>\n\n<p>It's ugly, but I think it will give you what you want. Too bad KeyValuePair is sealed.</p>\n"}, {'answer_id': 8833, 'author': 'Rob Cooper', 'author_id': 832, 'author_profile': 'https://Stackoverflow.com/users/832', 'pm_score': 2, 'selected': False, 'text': '<p>Just one thing to add to this (although I do think you have already had your question answered by others). In the interests of extensibility (since we all know it will happen at some point) you may want to check out the <a href="http://www.dofactory.com/Patterns/PatternComposite.aspx" rel="nofollow noreferrer">Composite Pattern</a> This is ideal for working with "Tree-Like Structures"..</p>\n\n<p>Like I said, I know you are only expecting one sub-level, but this could really be useful for you if you later need to extend ^_^</p>\n'}, {'answer_id': 8834, 'author': 'Shaun Austin', 'author_id': 1120, 'author_profile': 'https://Stackoverflow.com/users/1120', 'pm_score': 3, 'selected': False, 'text': '<p>I think what you might be after (as a literal implementation of your question), is:</p>\n\n<pre><code>public class TokenTree\n{\n    public TokenTree()\n    {\n        tree = new Dictionary&lt;string, IDictionary&lt;string,string&gt;&gt;();\n    }\n\n    IDictionary&lt;string, IDictionary&lt;string, string&gt;&gt; tree; \n}\n</code></pre>\n\n<p>You did actually say a "list" of key-values in your question, so you might want to swap the inner <code>IDictionary</code> with a:</p>\n\n<pre><code>IList&lt;KeyValuePair&lt;string, string&gt;&gt;\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/8800', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/61/']