Best implementation for a data structure of a pair of key values?

So, I got a little stuck with C # lately, and all the general collections were a bit embarrassing to me. Let's say I wanted to introduce a data structure where the head of the tree was a key pair of values, and then there is one optional list of key value pairs below (but no more levels than these). Would it be suitable?

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; } 

This is just a simple data shunt.

+68
collections c # data-structures
Aug 12 '08 at 13:12
source share
8 answers

There is an actual data type called KeyValuePair, use this

 KeyValuePair<string, string> myKeyValuePair = new KeyValuePair<string,string>("defaultkey", "defaultvalue"); 
+131
Aug 12 '08 at 13:20
source share

One possible thing you can do is use the Dictionary object right out of the box, and then just extend it with your own modifications:

 public class TokenTree : Dictionary<string, string> { public IDictionary<string, string> SubPairs; } 

This gives you the advantage that you do not need to apply IDictionary rules for your key (e.g. key uniqueness, etc.).

And you understood the concept of the designer :)

+12
Aug 12 '08 at 13:25
source share

I think you could after (as a literal implementation of your question):

 pubic class TokenTree { public TokenTree() { tree = new Dictionary<string, IDictionary<string,string>>(); } IDictionary<string, IDictionary<string, string>> tree; } 

You really said a “list” of key values ​​in your question so that you can change the internal IDictionary to:

 IList<KeyValuePair<string, string>> 
+7
Aug 12 '08 at 13:45
source share

There is a built-in type of KeyValuePair. Actually, this is what IDictionary gives you access to when you iterate over it.

In addition, this structure is hardly a tree; finding a more representative name can be a good exercise.

+5
Aug 12 '08 at 13:23
source share

Only one thing to add to this (although I think you already answered your question by others). In the interest of extensibility (since we all know that this will happen at some point), you might want to check out Composite Pattern. This is the perfect solution for working with "Tree-Like Structures".

As I said, I know that you expect only one sub-level, but it can really be useful for you if you need to expand ^ _ ^ later

+3
Aug 12 '08 at 13:45
source share

@ Jay Mooney : The .NET common class Dictionary is actually a hash table, only with fixed types.

The code you showed should not convince anyone to use a hashtable instead of a dictionary, since both parts of the code can be used for both types.

For a hash table:

 foreach(object key in h.keys) { string keyAsString = key.ToString(); // btw, this is unnecessary string valAsString = h[key].ToString(); System.Diagnostics.Debug.WriteLine(keyAsString + " " + valAsString); } 

For a dictionary:

 foreach(string key in d.keys) { string valAsString = d[key].ToString(); System.Diagnostics.Debug.WriteLine(key + " " + valAsString); } 

And just the same for the other with KeyValuePair, just use the non-generic version for the Hashtable and the generic version for the dictionary.

So, it is just as simple, but the Hashtable uses Object for both the key and the value, which means that you will enter all types of values ​​and you have no type safety, and the dictionary uses general types and is therefore better.

+2
Aug 12 '08 at 13:25
source share

A dictionary class is exactly what you want, right.

You can declare a field directly as a dictionary, not an IDictionary, but it is up to you.

+1
Aug 12 '08 at 13:22
source share

Use something like this:

 class Tree < T > : Dictionary < T, IList< Tree < T > > > { } 

This is ugly, but I think it will give you what you want. Too bad KeyValuePair sealed.

+1
Aug 12 '08 at 13:38
source share



All Articles