Best way to change dictionary key

No, you cannot rename keys once that have been added to a Dictionary. If you want a rename facility, perhaps add your own extension method:

public static void RenameKey<TKey, TValue>(this IDictionary<TKey, TValue> dic,
                                      TKey fromKey, TKey toKey)
{
  TValue value = dic[fromKey];
  dic.Remove(fromKey);
  dic[toKey] = value;
}

Leave a Comment