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);

반응형

/* 코드복사 버튼 */ pre { position: relative; overflow: visible; } pre .copy-button { opacity: 0; position: absolute; right: 8px; top: 4px; padding: 6px 18px; color: rgb(255, 255, 255); background: rgba(255, 223, 0, 0.6); border-radius: 5px; transition: opacity .3s ease-in-out; } pre:hover .copy-button { opacity: 1; } pre .copy-button:hover { color: #eee; transition: all ease-in-out 0.3s; } pre .copy-button:active { color: #33f; transition: all ease-in-out 0.1s; } .copy-message:before { content: attr(copy-message); position: absolute; left: -95px; top: 0px; padding: 6px 18px; color: #fff; background: rgba(255, 223, 0, 0.6); border-radius: 5px; } /* 코드복사 버튼 END */