HashSet<int> numbers = new HashSet<int>();
numbers.Add(1);
numbers.Add(2);
numbers.Add(3);
numbers.Add(3); // Duplicate element, ignored
Console.WriteLine(numbers.Count); // Output: 3
HashSet<string> colors = new HashSet<string> { "Red", "Green", "Blue" };
Console.WriteLine(colors.Contains("Yellow")); // Output: False
C#은 데이터를 효율적으로 저장하고 조작하는 데 사용할 수 있는 풍부한 데이터 구조 집합을 제공합니다. C#에서 가장 일반적인 데이터 구조는 다음과 같습니다.
Arrays
동일한 유형의 요소에 대한 고정 크기 모음입니다. 배열은 컬렉션의 크기를 미리 알고 있고 임의 액세스가 필요한 경우에 유용합니다.
int[] numbers = new int[] { 1, 2, 3, 4, 5 };
Console.WriteLine(numbers[2]); // Output: 3
string[] names = new string[3];
names[0] = "Alice";
names[1] = "Bob";
names[2] = "Charlie";
Console.WriteLine(names[1]); // Output: "Bob"
Lists
동적으로 크기를 조정할 수 있는 동일한 유형의 요소 모음입니다. 목록은 컬렉션의 크기가 시간이 지남에 따라 변경될 수 있고 순차 액세스가 필요한 경우에 유용합니다.
List<int> numbers = new List<int>();
numbers.Add(1);
numbers.Add(2);
numbers.Add(3);
Console.WriteLine(numbers[1]); // Output: 2
List<string> names = new List<string> { "Alice", "Bob", "Charlie" };
Console.WriteLine(names[2]); // Output: "Charlie"
Dictionaries
각 키가 고유한 키-값 쌍의 모음입니다. 사전은 키를 기반으로 값을 빠르게 조회해야 할 때 유용합니다.
Dictionary<string, int> ages = new Dictionary<string, int>();
ages.Add("Alice", 25);
ages.Add("Bob", 30);
ages.Add("Charlie", 35);
Console.WriteLine(ages["Bob"]); // Output: 30
Dictionary<int, string> colors = new Dictionary<int, string>
{
{ 1, "Red" },
{ 2, "Green" },
{ 3, "Blue" }
};
Console.WriteLine(colors[2]); // Output: "Green"
Sets
요소의 순서가 중요하지 않은 고유한 요소의 모음입니다. 합집합, 교집합, 차집합과 같은 집합 연산을 수행해야 할 때 집합이 유용합니다.
HashSet<int> numbers = new HashSet<int>();
numbers.Add(1);
numbers.Add(2);
numbers.Add(3);
numbers.Add(3); // Duplicate element, ignored
Console.WriteLine(numbers.Count); // Output: 3
HashSet<string> colors = new HashSet<string> { "Red", "Green", "Blue" };
Console.WriteLine(colors.Contains("Yellow")); // Output: False
Stacks
추가된 마지막 요소가 제거될 첫 번째 요소인 요소 모음입니다(후입선출). 스택은 요소가 추가된 순서를 추적해야 할 때 유용합니다.
Stack<int> numbers = new Stack<int>();
numbers.Push(1);
numbers.Push(2);
numbers.Push(3);
Console.WriteLine(numbers.Pop()); // Output: 3
Console.WriteLine(numbers.Peek()); // Output: 2
Queues
추가된 첫 번째 요소가 제거될 첫 번째 요소인 요소 모음입니다(선입선출). 대기열은 추가된 순서대로 요소를 처리해야 할 때 유용합니다.
Queue<string> names = new Queue<string>();
names.Enqueue("Alice");
names.Enqueue("Bob");
names.Enqueue("Charlie");
Console.WriteLine(names.Dequeue()); // Output: "Alice"
Console.WriteLine(names.Peek()); // Output: "Bob"
특정 작업에 사용할 데이터 구조를 결정할 때 컬렉션의 예상 크기, 컬렉션에서 수행될 작업의 빈도 및 유형, 작업의 성능 특성과 같은 작업 요구 사항을 고려하는 것이 중요합니다. 각 데이터 구조. 예를 들어 임의로 액세스되는 정수의 고정 크기 컬렉션을 저장해야 하는 경우 배열이 최선의 선택일 수 있습니다. 반면에 순차적으로 액세스할 동적으로 크기를 조정할 수 있는 문자열 모음을 저장해야 하는 경우 목록이 더 적합할 수 있습니다. 당면한 작업에 적합한 데이터 구조를 선택하면 코드를 효율적이고 쉽게 유지 관리할 수 있습니다.
'개발자 이야기' 카테고리의 다른 글
언리얼과 유니티의 가비지 컬렉션 메모릭 관리 방법의 차이점? (0) | 2023.03.21 |
---|---|
C# 코드에서 예외를 어떻게 처리합니까? (0) | 2023.03.21 |
Unity에서 코루틴과 일반 메서드의 차이점을 설명해 주시겠습니까? (0) | 2023.03.20 |
C#에서 값 유형과 참조 유형의 차이점을 설명할 수 있습니까? (0) | 2023.03.20 |
c# null 조건부 연산자 "?" (0) | 2023.03.18 |