5

I have a dictionary < string,object > which has a mapping of a string and a dictionary < string,int >. How do I add a key value pair in the inside dictionary < string ,int > ?

Dictionary <string,object> dict = new Dictionary <string,object>();
Dictionary <string,int> insideDict = new Dictionary <string,int>();
// ad some values in insideDict
dict.Add("blah",insideDict);

So now the dict has a dictionary mapped with a string.Now I want to separately add values to the insideDict. I tried

dict["blah"].Add();

Where am I going wrong?

bazzi
  • 559
  • 1
  • 8
  • 23
  • @GiladGreen Added. – bazzi Jun 27 '16 at 13:55
  • Compare it to my answer. The comment you added before the top .Add isn't correct - that line adds to the outer dictionary an item with the key "blah" and the insideDict as it's value. The second .Add - you need to give that function some parameters - it requires another key and value (for the inner dictionary) – Gilad Green Jun 27 '16 at 13:58
  • @GiladGreen That comment was for the 2nd statement meaning to add some key value pairs for innerDict – bazzi Jun 27 '16 at 14:03

3 Answers3

9

Do you mean something like this?

var collection = new Dictionary<string, Dictionary<string, int>>();
collection.Add("some key", new Dictionary<string, int>());
collection["some key"].Add("inner key", 0);
Gilad Green
  • 36,708
  • 7
  • 61
  • 95
  • I want to be able to add new key value pairs to the innerDict. – bazzi Jun 27 '16 at 13:58
  • Just like my third line. When I did collection["some key"] I got the reference of the value of that key - which is the string to int dictionary. and then just .Add("some other key", 2); – Gilad Green Jun 27 '16 at 14:00
5

Something like below

Dictionary<string, object> dict = new Dictionary<string, object>();
dict.Add("1", new Dictionary<string, int>());

(OR) if you already have defined the inner dictionary then

Dictionary<string, object> dict = new Dictionary<string, object>();
Dictionary<string, int> innerdict = new Dictionary<string, int>();
dict.Add("1", innerdict); // added to outer dictionary
string key = "1";
((Dictionary<string, int>)dict[key]).Add("100", 100); // added to inner dictionary

Per your comment tried this but screwed up somewhere

You didn't got it cause of your below line where you forgot to cast the inner dictionary value to Dictionary<string, int> since your outer dictionary value is object. You should rather have your outer dictionary declared strongly typed.

dict.Add("blah",insideDict); //forgot casting here
Gilad Green
  • 36,708
  • 7
  • 61
  • 95
Rahul
  • 76,197
  • 13
  • 71
  • 125
  • I want to be able to add new key value pairs to the innerDict where innerDict already has some values. – bazzi Jun 27 '16 at 13:58
  • @vishallc, added an explanation as why you got screwed up. you may check the answer again. – Rahul Jun 27 '16 at 14:07
0
Dictionary<string, Dictionary<string,TValue>> dic = new Dictionary<string, Dictionary<string,TValue>>();

Replace the TValue with your value type.

Paul Swetz
  • 2,234
  • 1
  • 11
  • 28