How is the c#/.net 3.5 dictionary implemented?

Viewed 27359

I'm using an application which uses a number of large dictionaries ( up to 10^6 elements), the size of which is unknown in advance, (though I can guess in some cases). I'm wondering how the dictionary is implemented, i.e. how bad the effect is if I don't give an initial estimate of the dictionary size. Does it internally use a (self-growing) array in the way List does? in which case letting the dictionaries grow might leave a lot of large un-referenced arrays on the LOH.

5 Answers
  • JSON as dictionary
{
 "Details": 
    { 
    "ApiKey": 50125
    }
}
  • Model should contain property as type Dictionary.
public Dictionary<string, string> Details{ get; set; }
  • Implement foreach() block with datatype as "KeyValue"
       foreach (KeyValuePair<string, string> dict in Details)
                {
                    switch (dict.Key)
                    {
                      case nameof(settings.ApiKey):
                      int.TryParse(kv.Value, out int ApiKey);
                      settings.ApiKey=ApiKey;
                            break;
                        default:
                            break;
                       }
                   }

Related