現時点では、これ以外に方法がありません(間接的な更新):
private void UpdateKey(Dictionary<string,object> dict, string oldKey, string newKey){
if(dict.ContainsKey(oldKey)){
object value = dict[oldKey];
dict.Remove(oldKey);
dict.Add(newKey,value);
}
}
他に良い方法はありますか?
もちろん、上記のメソッドは単なる単純なものです。例外をスローせずにうまく機能させるには、辞書内の既存のキーとの重複について newKey をチェックする必要があります。このような:
private void UpdateKey(Dictionary<string,object> dict, string oldKey, string newKey){
if(dict.ContainsKey(oldKey)){
object value = dict[oldKey];
dict.Remove(oldKey);
dict[newKey] = value;
}
}
事前にどうもありがとうございました!