Reputation: 113
I wrote the following code structure:
public Dictionary<string, Dictionary<string, Object>> file_data = new Dictionary<string, Dictionary<string, Object>>();
public Form1()
{
InitializeComponent();
Dictionary<string, Object> temporary = new Dictionary<string, Object>();
temporary.Add("content", null);
temporary.Add("droplist", false);
temporary.Add("valid_file", false);
file_data.Add("base_data", temporary);
file_data.Add("supplier_data_1", temporary);
file_data["base_data"]["droplist"] = true;
MessageBox.Show(file_data["supplier_data_1"]["droplist"].ToString());
}
I just wanted to update ["base_data"]["droplist"]
, but the code updates the ["supplier_data_1"]["droplist"]
value as well (messagebox shows true). Why is it doing that? How do I have to adapt the code that the file_data["base_data"]["droplist"]
will be changed only? The Dictionary
has to keep it's structure.
Upvotes: 2
Views: 136
Reputation: 2284
As @hardkoded said, it's a reference type.
// Here you define `temporal` object
Dictionary<string, Object> temporary = new Dictionary<string, Object>();
// Here you add a reference/pointer to the `temporal` object as values to these keys
file_data.Add("base_data", temporary);
file_data.Add("supplier_data_1", temporary);
// When you get the values for the "base_data" and "supplier_data_1" keys, you get a refence to the `temporal`object.
var baseData = file_data["base_data"];
var supplierData1 = file_data["supplier_data_1"];
// If you compare those variables, you'll find out that they are references to the same object!
if (baseData == supplierData1) // true
Debug.WriteLine("They are the same!");
So, any changes made to file_data["base_data"]
will apply to file_data["supplier_data_1"]
as they are currently references/pointers to the same object!
Upvotes: 0
Reputation: 21617
Object
is a reference type. Microsoft Value Types post has a good explanation
A variable of a value type contains a value of the type. For example, a variable of the int type might contain the value 42. This differs from a variable of a reference type, which contains a reference to an instance of the type, also known as an object. When you assign a new value to a variable of a value type, that value is copied. When you assign a new value to a variable of a reference type, the reference is copied, not the object itself.
You should clone that dictionary. Like it's explained here. You could do something like this:
file_data.Add("supplier_data_1", CloneDictionary(temporary));
Or even better, create a new Dictionary based on the first one.
file_data.Add("supplier_data_1", new Dictionary<string, object>(temporary));
Upvotes: 4
Reputation: 218847
You're adding the same object twice:
file_data.Add("base_data", temporary);
file_data.Add("supplier_data_1", temporary);
The temporary
variable points to an object in memory. Now two references in the dictionary also point to that object in memory. But there's still only one object in memory. Any update made to that object will be seen by any reference to that object.
In order to have two different objects in the dictionary, you need to create a second object. Which you might do manually by creating two variables in your code, populating each of them, and adding them to that resulting dictionary. Or you might also just create a new dictionary around the same values if they're all simple value types:
file_data.Add("supplier_data_1", temporary.ToDictionary(x => x.Key, x => x.Value));
Upvotes: 0