컬렉션은 배열과는 달리 데이터의 추가 및 삭제등으로 변경됨에 따라 개체가 동적으로 확장되거나 축소될 수 있습니다.
C# 컬렉션 클래스의 유형
c#은 3개의 다양한 컬렉션을 제공하며 각 클래스에 대해 알아봅니다.
System.Collections.Generic
System.Collections
System.Collections.Concurrent
System.Collections.Generic 클래스
System.Collections.Generic네임스페이스의 클래스 중 하나를 사용하여 제네릭 컬렉션을 만들 수 있습니다. 제네릭 컬렉션은 컬렉션의 모든 항목에 동일한 데이터 형식이 있는 경우에 유용합니다. 제네릭 컬렉션은 원하는 데이터 형식만 추가할 수 있도록 하여 강력한 형식 지정을 적용합니다.
Dictionary, List, Queue, Stack, SortedList
■ 기본 사용법
List<T> 인덱스를 사용하여 액세스할 수 있는 동일한 데이터 유형List 의 여러 요소를 저장하는 데 사용됩니다 . 목록 내부에 요소를 추가, 삽입 및 제거할 수 있습니다. 또한 목록의 크기를 동적으로 변경할 수 있습니다.
using System;
using System.Collections.Generic;
// LIST
IList<string> list = new List<string>();
list.Add("cat");
list.Add("dog");
Console.WriteLine($"Access List Elements : {list[0]}");
// (index=0) 에 fish 를 추가
list.Insert(0, "fish");
foreach (var item in list)
{
Console.WriteLine($"Iterate the List item : {item}");
}
Dictionary<TKey, TValue> 키에 따라 구성된 키/값 쌍의 컬렉션을 나타냅니다.
using System;
using System.Collections.Generic;
IDictionary<int, string> dic = new Dictionary<int, string>();
dic.Add(1,"cat");
dic.Add(2,"dog");
dic[2] = "snake"; // value 값 변경
dic.Remove(1); // 키 값 삭제
if (dic.ContainsKey(2)) // 키가 있는지 확인
{
Console.WriteLine("Dictionary Key is already added..");
}
foreach (var item in dic)
{
Console.WriteLine($"Iterate the Dictionary : {item.Key}-{item.Value}");
}
Queue<T> FIFO(선입 선출) 방식으로 저장됩니다. 여기서 요소는 한쪽 끝에서 삽입되고 다른 쪽 끝에서 제거됩니다.
using System;
using System.Linq;
using System.Collections.Generic;
Queue<string> queue = new Queue<string>();
queue.Enqueue("cat" );
queue.Enqueue("dog" );
queue.Enqueue("fish" );
foreach (var item in queue)
{
Console.WriteLine($"Iterate the Queue : {item}");
}
Console.WriteLine($"Dequeue the Queue : {queue.Dequeue()}, Queue Cnt:{queue.Count()}"); // queue 에서 하나 제거
Console.WriteLine($"Peek the Queue : {queue.Peek()}, Queue Cnt:{queue.Count()}"); // peek 데이터만 조회
foreach (var item in queue)
{
Console.WriteLine($"Iterate the Queue Dequeue: {item}");
}
Stack<T> LIFO(Last In First Out) 방식으로 저장됩니다.
using System;
using System.Linq;
using System.Collections.Generic;
Stack<string> stack = new Stack<string>();
stack.Push("cat" );
stack.Push("dog" );
stack.Push("fish" );
foreach (var item in stack)
{
Console.WriteLine($"Iterate the Stack : {item}");
}
Console.WriteLine($"Pop the Stack : {stack.Pop()}, Queue Cnt:{stack.Count()}"); // stack 에서 하나 제거
Console.WriteLine($"Peek the Stack : {stack.Peek()}, Queue Cnt:{stack.Count()}");
foreach (var item in stack)
{
Console.WriteLine($"Iterate the Stack Pop : {item}");
}
SortedList<TKey, TValue> 연관된 IComparer 구현을 기반으로 키에 따라 정렬된 키/값 쌍의 컬렉션을 나타냅니다.
using System;
using System.Collections.Generic;
SortedList<string, string> sortList = new SortedList<string, string>();
sortList.Add("1", "cat" );
sortList.Add("2", "dog" );
sortList.Add("3", "fish" );
foreach (var item in sortList)
{
Console.WriteLine($"Iterate the SortedList : {item.Key}-{item.Value}");
}
if (sortList.ContainsKey("2")) // 키가 있는지 확인
{
Console.WriteLine("Key is already added..");
}
sortList.Remove("2"); // key 값이 2 삭제
foreach (var item in sortList)
{
Console.WriteLine($"Iterate the SortedList Remove : {item.Key}-{item.Value}");
}
System.Collections 클래스
System.Collections 네임스페이스의 클래스는 구체적 형식의 개체가 아니라 Object 형식의 개체로 요소를 저장합니다.