해시 테이블은 키(Key)를 해시 함수(Hash Function)에 넣어 계산된 인덱스(Index)에 값(Value)을 저장하는 자료구조
O(1)ex) Key → 해시 함수 → 인덱스 → 배열[인덱스] = Value
서로 다른 키들이 같은 해시 값을 갖는 현상
➡ 즉, 같은 인덱스를 가리키는 경우
➡ C#에서 사용되는 방식
충돌 시 다음 인덱스를 탐색하여 빈 자리에 저장
Dictionary<TKey, TValue>C#에서는 System.Collections.Generic.Dictionary<TKey, TValue>를 사용하여 해시 테이블을 구현함.
// Dictionary<key 값, value값> 변수 = new Dictionary<key,value)();
Dictionary<string,Monster> monsterDic = new Dictionary<string, Monster>();
O(1)에 가까운 성능으로 빠른 데이터 접근 가능Dictionary<string, int> dict = new Dictionary<string, int>();
dict.Add("Apple", 100);
// dict.Add("Apple", 200); → 예외 발생
📌 ContainsKey
특정 키 존재 여부 확인
{
Console.WriteLine("Key exists!");
}
dict.Remove("Apple");
if (dict.TryGetValue("Apple", out int value))
{
Console.WriteLine($"Value: {value}");
}