본문 바로가기
2/[ C# ]

[C#] Dictionary Key Rename, Dictionary Key 이름 변경

by Kieran_Han 2022. 5. 24.

Stack Overflow 검색하다보니, Dictionary Function 중 Key 이름 변경 Function은 없다고한다.

그래서 어떤 사람이 Method 만들어서 Dictionary Key 이름 변경하는 법을 보여줬다.

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

 

위 함수를 사용할 경우,

RenameKey(dict_Batch_1, "Description", "Description: Date/Tag");

 

로 입력하면 내가 원하는 Tag명을 새롭게 변경할 수 있다.