For a dictionary with lists as values, changing value associated with one key changes value for all other keys?
The code below is intended to update the values in a dictionary called BucketDict, that has integer keys and list of integers as values. But it appears as if editing the value for one key edits the rest as well?
Resetting the dictionary:
BucketDict.Clear();
List<int> val = new List<int>();
for (int i = 0; i < nBucket; i++)
{
BucketDict.Add(i, val);
}
Updating values:
int key;
int x;
int y;
List<int> val = new List<int>(); // intended to hold some value from the dictionary
for (int i = 0; i < MaxCommunitySize; i++) {
x = (int)(LocationX[i] * BucketConversionFactor);
y = (int)(LocationY[i] * BucketConversionFactor);
key = x + y * ArenaLengthInBuckets;
val = BucketDict[key]; //intended to 'write over' previous value
val.Add(i);
Bucket[i] = key;
BucketDict[key] = val;
}
I don't understand exactly what you're trying to do here. What are you expecting to see? Lists are reference types, so every time you iterate through the for loop, you are making 'val' refer to the entire list referenced by 'key' in the 'BucketDict' dictionary and then you are adding another int to the list with the value of i.
BucketDict[key] - is a list of ints. Let's say that it equals 1,2,3,4,5 val = BucketDict[key] - makes val refer to the list above val.add(i) - adds the value of i to the end of the above list. If i is 2 then the list is now 1,2,3,4,5,2 BucketDict[key] = val - doesn't really do anything because val is already a reference to BucketDict[key]
In your example here, you would get the same result if you got rid of the val list and, replaced line 16 with BucketDict[key].add(i);
Thanks @Ahndrakhul and see answer below. Thanks regarding the efficient way to write line 16 as well.
Answer by bdalziel · May 25, 2018 at 06:47 PM
@Ahndrakhul thanks for responding to a confused question. What you said did lead me to the answer (I have also edited the question to make it clearer what my problem was):
The overall problem was that I needed to read about reference types versus value types in C#. Now that the problem is fixed I can see that the problem was where the dictionary was 'reset', and that code was not included in the original question - it is now.
I was trying to initially set each value in the dictionary to an empty list, but because list is a reference type, I had set each value to the same list, so subsequently changing one entry in the dictionary changed the others as well.
When resetting the dictionary, declaring a new list in each iteration of the for loop fixes the problem
BucketDict.Clear();
for (int i = 0; i < nBucket; i++)
{
List<int> val = new List<int>();
BucketDict.Add(i, val);
}