Sorted Set는 C#의 정렬된 집합을 나타내는 자료구조중 하나이다.
HashSet처럼 중복이 허용되지 않고 자동으로 정렬된다는 것이 특징이다.
SortedSet<int> scores = new SortedSet<int>();
scores.Add(50);
scores.Add(20);
scores.Add(70);
scores.Add(20); // 중복 추가: 무시됨
foreach (int score in scores)
{
Debug.Log(score);
}
위 코드는 20, 50, 70으로 중복 무시와 정렬이 자동으로 된다.
Remove는 요소를 제거하며 성공 여부를 bool값으로 반환한다.
SortedSet<int> numbers = new SortedSet<int>() { 10, 20, 30, 40 };
bool removed = numbers.Remove(20);
위 코드는 요소를 삭제를 시도하고 삭제에 성공하였기에 removed는 true가 된다.
SortedSet<int> numbers = new SortedSet<int>() { 10, 20, 30, 40 };
numbers.Clear();
Clear는 모든 요소를 삭제 시켜 초기화한다.