简体   繁体   中英

Strange Behavior of Nested Dictionary in Unity C#

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);

Thats because you put the same reference to the roundVals dictionary object in the seenValsRounds dictionary. You should create a new dictionary for A, B, C and D.

It's may be helpful or Nested dictionary alternative.

Create Sample.cs script and test it.

 public Dictionary<string,Tuple<string,string,string,int>> _planets = new 
     Dictionary<string, Tuple<string,string, string, int>>();
     void Start()
     {
           string myKey = string.Concat("1","MetalMine","Level");
           if(!_planets.ContainsKey(myKey))
           {
               _planets.Add(myKey,Tuple.Create("1","MetalMine","Level",0));
           }
           Debug.Log("_planets mykey "+myKey+" ==> "+_planets[myKey].Item4);
     }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM