I have a dictionary with int keys, and int Lists as values. How can I copy this by value?
I did the following, but it copies the List part by reference:
Dictionary<int, List<int>> d2 = new Dictionary<int, List<int>>(d1);
You could use ToDictionary
to copy the dictionary and ToList
to copy lists:
var dNew = d1.ToDictionary(x => x.Key, x => x.Value.ToList());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With