C#C
C#3y ago
14 replies
MightyPirate

✅ data override in dictionary of dictionaries

I'm trying to create a dictionary of dictionaries, and use temp variable to create it, but when i change data in temp variable, it also changes in main dictionary.
My code looks like this:

tempDictionary.Add(key, stuff)
tempDictionary.Add(key2, stuff2)
mainDictionary.Add(mkey, tempDictionary)
tempDictionary.Clear
tempDictionary.Add(key3, stuff3)
mainDictionary.Add(mkey2, tempDictionary)

the problem is mainDictionary[mkey] turns out to be the same as mainDictionary[mkey2]

What are my options?
Was this page helpful?