C#
[C#]C# Dictionary <TKey,TValue> 클래스 배열 데이터 사용법
데브프로그라
2024. 4. 16. 20:04
반응형
[C#]C# Dictionary <TKey,TValue> 클래스 배열 데이터 사용법
● Dictionary 키와 값의 컬렉션을 나타냅니다.
네임스페이스: System.Collections.Generic
어셈블리: Systehttp://m.Collections.dll
TKey - 사전에 있는 키의 형식입니다.
TValue - 사전에 있는 값의 형식입니다.
가. Add - 지정한 키와 값을 추가한다.
Dictionary<int, string> dicList = new Dictionary<int, string>();
dicList.Add(1, "빨강");
dicList.Add(2, "노랑");
dicList.Add(3, "녹색");
dicList.Add(4, "파랑");
나. KeyValuePair - 키/값 조회 확인한다.
foreach(KeyValuePair<int, string> each in dicList)
{
Console.WriteLine("Key=>{0}, Value=>{1}", each.Key, each.Value);
}
다. ContainKey - Key값이 존재하는지 확인한다.
if(dicList.ContainKey(2))
Console.WriteLine("2 Key가 존재합니다.");
else
Console.WriteLine("2 Key가 존재하지 않습니다.");
라. Remove - 지정한 키 값을 제거한다.
dicList.Remove(2);
반응형