반응형
    
    
    
  C# 콜렉션 중 하나인 Dictionary에 대해 알아보겠습니다.
<Dictionary 선언하는 방법>
Dictionary<string, int> dictionary = new Dictionary<string, int>();
Dictionary를 선언할 때는 <Key, Value>를 이용해 선언합니다. 즉, 키와 값이 쌍으로 이루어져 만들어져 있습니다. 차후 값을 읽어올 때에도 이 점을 이용하게되므로 아주 중요한 개념입니다. 예제를 통해 알아보겠습니다.
[1] Dictionary 읽고 쓰는 기본적인 사용 방법
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33  | using System; using System.Collections.Generic; namespace DictionarySample {     class Program     {         Static void Main()         {             Dictionary<string, int> dic = new Dictionary<string, int>()             {                 {"dog", 2},                 {"iguana", -1},                 {"cat", 1},                 {"cow", 0}             };             foreach (KeyValuePair<string, int> pair in dic)             {                 Console.WriteLine("{0}, {1}",                     pair.Key,                     pair.Value);             }             foreach (var pair in d)             {                 Console.WriteLine("{0}, {1}",                     pair.Key,                     pair.Value);             }         }     } }  | cs | 
Output
dog, 2
iguana, -1
cat, 1
cow, 0
dog, 2
iguana, -1
cat, 1
cow, 0
[2] Key를 List로 받아와서 처리하는 샘플 코드
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28  | using System; using System.Collections.Generic; namespace DictionarySample {     class DictionaryToList     {         public void DictionaryToListFunc()         {             Dictionary<string, int> dic = new Dictionary<string, int>()             {                 {"dog", 2},                 {"iguana", -1},                 {"cat", 1},                 {"cow", 0}             };             List<string> list = new List<string>(dic.Keys);             foreach (string k in list)             {                 Console.WriteLine("{0}, {1}",                     k,                     d[k]);             }         }     } }  | cs | 
Output
dog, 2
iguana, -1
cat, 1
cow, 0
# 두 코드의 성능 차이 #
1. KeyValuePair Loop : 125 ms
2. Keys Loop : 437 ms
-> 키 리스트로 만든 이후에 이 키를 이용해 다시 루프를 돌면서 값을 읽어 오는 것이 훨씬 효율적입니다.
[3] 키 또는 값 있는지 확인하기
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22  | public void ContainsKeyFunc() {     Dictionary<int, string> dict = new Dictionary<int, string>();     dict.Add(100, "Jhon");     dict.Add(200, "Steven");     if (dict.ConstainKey(200))     {         Console.WriteLine(true);     } } public void ContainsValueFunc() {     Dictionary<string, int> dict = new Dictionary<string, int>();     dict.Add("dog", 1);     dict.Add("cow", 2);     if (dict.ContainsValue(1))     {         Console.WriteLine(true);     } }  | cs | 
위의 코드는 Key가 있는지 확인하는 코드, 아래 코드는 Value가 있는지 확인하는 코드
[4] LINQ를 이용해서 Array를 Dictionary로 변환하기
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26  | using System; using System.Collections.Generic; using System.Linq; namespace DictionarySample {     class DictionaryToList     {         public void DictionaryLINQ()         {             string[] arr = new string[]             {                 "Hello",                 "World"             };             var dict = arr.ToDictionary(item => item, item => true);             foreach (var pair in dict)             {                 Console.WriteLine("{0}, {1}",                     pair.Key,                     pair.Value);             }         }     } }  | cs | 
[5] Dictionary Indexer
Dictionary를 array처럼 Index를 이용해 사용할 수 있습니다.
1 2 3 4 5 6 7 8 9 10 11  | public void DictionaryIndexers() {     Dictionary<int, int> dictionary = new Dictionary<int, int>();     dictionary[1] = 2;     dictionary[2] = 1;     dictionary[3] = 3;     Console.WriteLine(dictionary[1]);     Console.WriteLine(dictionary[2]); }  | cs | 
Output
3
1
참고한 사이트 : http://swconsulting.tistory.com/40
반응형
    
    
    
  '- Programming > - C#' 카테고리의 다른 글
| ★ 18. 람다식 (Lambda Expression) (0) | 2017.03.26 | 
|---|---|
| ★ 16. C# - List 사용 예제 (2) | 2017.03.22 | 
| ★ 15. c# ref와 out의 차이 (0) | 2017.02.16 | 
| ★ 14. c# 네트워크 개발 p13 (0) | 2017.02.16 | 
| ★ 13. c# 네트워크 개발 p12 (0) | 2017.02.16 |