I am using a dictionary within a dictionary. The last key value assigned is getting stored as values for all previous keys as well, even though the individual key assignments are different. Am I missing something?
Dictionary<string, Dictionary <int,bool>> seenValsRounds= new Dictionary<string, Dictionary<int, bool>>();
void prepareRoundsVals()
{
Dictionary <int,bool> roundVals = new Dictionary<int, bool> ();
roundVals.Add (0,false);
seenValsRounds.Add ("A", roundVals);
seenValsRounds.Add ("B", roundVals);
seenValsRounds.Add ("C", roundVals);
seenValsRounds.Add ("D", roundVals);
seenValsRounds ["A"] [0] = false;
seenValsRounds ["B"] [0] = false;
seenValsRounds ["C"] [0] = false;
seenValsRounds ["D"] [0] = true;
foreach (KeyValuePair<string, Dictionary<int,bool>> kvp in seenValsRounds) {
Debug.Log(kvp.Key + " in round " + 0 + ": " + seenValsRounds [kvp.Key][0]);
}
}
Expected Results: A is false, B is false, C is false, D is True
Actual Results: A is True, B is True, C is True, D is True
Solved below as per suggestions from answers and comments. Each nested dictionary should also be 'new':
Dictionary <int,bool> roundVals1 = new Dictionary<int, bool> ();
Dictionary <int,bool> roundVals2 = new Dictionary<int, bool> ();
Dictionary <int,bool> roundVals3 = new Dictionary<int, bool> ();
Dictionary <int,bool> roundVals4 = new Dictionary<int, bool> ();
roundVals1.Add (0,false);
roundVals2.Add (0,false);
roundVals3.Add (0,false);
roundVals4.Add (0,false);
seenValsRounds.Add ("A", roundVals1);
seenValsRounds.Add ("B", roundVals2);
seenValsRounds.Add ("C", roundVals3);
seenValsRounds.Add ("D", roundVals4);