mnj
mnj

Reputation: 3413

Can't add value into Dictionary containing dictionaries of dictionaries

I have a dictionary:

public Dictionary<string,Dictionary<string, Dictionary<DateTime, float>>> CPUResults = new Dictionary<string,Dictionary<string, Dictionary<DateTime, float>>>();

Now, I'd like to add some data:

CPUResults.Add("KEY 1", new Dictionary<string, Dictionary <DateTime, float>>().Add("key 1", new Dictionary<DateTime,float>()));

Actually, i don't want my keys to be: key 1, key 2, etc. - just not to complicate I named those keys like this.

Well, important thing is, this won't compile. I have no idea what I did wrong - it took me a while to come up with this, but still it doesn't work.

Upvotes: 0

Views: 67

Answers (1)

Tomas Aschan
Tomas Aschan

Reputation: 60564

The Dictionary<TKey, TValue>.Add method doesn't return the dictionary. Instead, initialize the inner dictionary like this:

CPUResults.Add(
    "KEY 1",
    new Dictionary<string, Dictionary <DateTime, float>>
    {
        { "key 1", new Dictionary<DateTime,float>() } 
    });

Upvotes: 7

Related Questions