My idea was to copy a dictionary while resetting all the values of the previous one, so i have this instruction:
var dic2 = new Dictionary<string, int>(dic.ToDictionary(kvp => kvp.Key, kvp => 0));
However i had an unexpected problem doing this, since the new copied dictionary doesnt have the same order of keys of the previous one.
Any way to reset the values but to maintain the same order of keys? Witouth resorting to some type of sorting?
The answer is not to rely on the order of keys in a
Dictionary<,>in the first place. It’s emphatically not guaranteed.This is documented on MSDN, but not nearly as clearly as I’d have wanted:
.NET doesn’t currently have an insertion-order-preserving dictionary implementation as far as I’m aware 🙁