Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the quickest way to compare a C# Dictionary to a 'gold standard' Dictionary for equality?
    primarykey
    data
    text
    <p>I have a known-good Dictionary, and at run time I need to create a new Dictionary and run a check to see if it has the same key-value pairs as the known-good Dictionary (potentially inserted in different orders), and take one path if it does and another if it doesn't. I don't necessarily need to serialize the entire known-good Dictionary (I could use a hash, for example), but I need some on-disk data that has enough information about the known-good Dictionary to allow for comparison, if not for recreation. What is the quickest way to do this? I can use a SortedDictionary, but the amount of time required to initialize and add values counts in the speed of this task.</p> <p>Concrete example:</p> <p>Consider a Dictionary<code>&lt;String,List&lt;String&gt;&gt;</code> that looks something like this (in no particular order, obviously): </p> <pre><code>{ {"key1", {"value1", "value2"} }, {"key2", {"value3", "value4"} } } </code></pre> <p>I create that Dictionary once and save some form of information about it on disk (a full serialization, a hash, whatever). Then, at runtime, I do the following:</p> <pre><code>Dictionary&lt;String,List&lt;String&gt;&gt; d1 = new Dictionary&lt;String,List&lt;String&gt;&gt; (); Dictionary&lt;String,List&lt;String&gt;&gt; d2 = new Dictionary&lt;String,List&lt;String&gt;&gt; (); Dictionary&lt;String,List&lt;String&gt;&gt; d3 = new Dictionary&lt;String,List&lt;String&gt;&gt; (); String key11 = "key1"; String key12 = "key1"; String key13 = "key1"; String key21 = "key2"; String key22 = "key2"; String key23 = "key2"; List&lt;String&gt; value11 = new List&lt;String&gt; {"value1", "value2"}; List&lt;String&gt; value12 = new List&lt;String&gt; {"value1", "value2"}; List&lt;String&gt; value13 = new List&lt;String&gt; {"value1", "value2"}; List&lt;String&gt; value21 = new List&lt;String&gt; {"value3", "value4"}; List&lt;String&gt; value22 = new List&lt;String&gt; {"value3", "value4"}; List&lt;String&gt; value23 = new List&lt;String&gt; {"value3", "value5"}; dict1.add(key11, value11); dict1.add(key21, value21); dict2.add(key22, value22); dict2.add(key12, value12); dict3.add(key13, value13); dict3.add(key23, value23); dict1.compare(fileName); //Should return true dict2.compare(fileName); //Should return true dict3.compare(fileName); //Should return false </code></pre> <p>Again, if the overall time from startup to the return from compare() is quicker, I can change this code to use a SortedDictionary (or anything else) instead, but I can't guarantee ordering and I need some consistent comparison. compare() could load a serialization and iterate through the dictionaries, it could serialize the in-memory dictionary and compare the serialization to the file name, or it could do any number of other things.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload