박하의 나날

Dictionary<TKey, TValue>

프로그래밍/Unity

 

Dictionary<TKey, TValue>

2016.10.04. 17:52

Dictionary<TKey, TValue>

키와 값의 컬렉션을 나타낸다.

Add  지정한 키와 값을 가지는 요소를 추가
Remove  지정한 키를 가지는 요소를 제거

 

using System;
using System.Collections.Generic;
public class FileFormatTable{
    private Dictionary<string, string> openWith;
    
    public FileFormatTable(){
        openWith = new Dictionary<string, string>();
        
        //추가
        openWith.Add("txt", "notepad.exe");
        openWith.Add("bmp", "paint.exe");
        openWith.Add("dib", "paint.exe");
        openWith.Add("rtf", "wordpad.exe");
        openWith.Add("odf", "wordpad.exe");
        //이미 할당 된 키일 경우
        try{
            openWith.Add("txt", "windword.exe");
        }
        catch{
            Console.Writeline("an element with Key = \"txt\" already exists.");
        }
        //제거
        openWith.Remove("odf");
    }
    public void printTable(){
        //탐색
        foreach (KeyValuePair<string, string> d in openWith)
            Console.WriteLine("key = {0}, Value = {1}", d.Key, d.Value);
    }
}
class MainClass
{
    public static vodi Main(string[] args)
    {
        FileFormatTable fileFormatTable = new FileFormatTable();
        fileFormatTable.printTable();
    }
}
 
cs

'프로그래밍 > Unity' 카테고리의 다른 글

Stack<T>  (0) 2017.04.09
Queue<T>  (0) 2017.04.09
List<T>  (0) 2017.04.09
LinkedList<T>  (0) 2017.04.09
Generic(일반화), Collection  (0) 2017.04.09