C#의 SortedList에서 지정된 키를 가진 요소를 제거하려면 Remove() 메서드를 사용합니다. 이 메서드는 인자로 전달된 키와 일치하는 요소를 SortedList에서 삭제하며, 일치하는 키가 존재하지 않으면 컬렉션에 아무런 변화도 주지 않습니다. 단, 키로 null을 전달하면 ArgumentNullException이 발생하므로 주의해야 합니다.
참고로 Remove() 메서드는 키를 기준으로 요소를 삭제하지만, 인덱스 위치를 기준으로 삭제하고 싶다면 RemoveAt() 메서드를 사용하면 됩니다.
예제 1
아래 예제에서는 SortedList에 A부터 J까지의 키와 값을 차례로 추가한 뒤, Remove() 메서드를 사용해 키 "H"에 해당하는 요소를 제거하고 그 결과를 확인합니다.
using System;
using System.Collections;
public class Demo {
public static void Main(String[] args) {
SortedList sortedList = new SortedList();
sortedList.Add("A", "1");
sortedList.Add("B", "2");
sortedList.Add("C", "3");
sortedList.Add("D", "4");
sortedList.Add("E", "5");
sortedList.Add("F", "6");
sortedList.Add("G", "7");
sortedList.Add("H", "8");
sortedList.Add("I", "9");
sortedList.Add("J", "10");
Console.WriteLine("SortedList elements...");
foreach(DictionaryEntry d in sortedList) {
Console.WriteLine("Key = "+d.Key + ", Value = " + d.Value);
}
Console.WriteLine("Count of SortedList key-value pairs = "+sortedList.Count);
sortedList.Remove("H");
Console.WriteLine("\nEnumerator to iterate through the SortedList...");
IDictionaryEnumerator demoEnum = sortedList.GetEnumerator();
while (demoEnum.MoveNext())
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
Console.WriteLine("Count of SortedList key-value pairs (Updated) = "+sortedList.Count);
}
}
출력 결과
위 프로그램을 실행하면 다음과 같은 결과가 출력됩니다.
SortedList elements... Key = A, Value = 1 Key = B, Value = 2 Key = C, Value = 3 Key = D, Value = 4 Key = E, Value = 5 Key = F, Value = 6 Key = G, Value = 7 Key = H, Value = 8 Key = I, Value = 9 Key = J, Value = 10 Count of SortedList key-value pairs = 10 Enumerator to iterate through the SortedList... Key = A, Value = 1 Key = B, Value = 2 Key = C, Value = 3 Key = D, Value = 4 Key = E, Value = 5 Key = F, Value = 6 Key = G, Value = 7 Key = I, Value = 9 Key = J, Value = 10 Count of SortedList key-value pairs (Updated) = 9
실행 결과를 보면 Remove("H") 호출 이후 키 "H"를 가진 요소가 목록에서 사라졌으며, 키-값 쌍의 개수도 10개에서 9개로 줄어든 것을 확인할 수 있습니다.
예제 2
이번에는 문자열 키를 사용하는 또 다른 예제를 살펴보겠습니다. Remove() 메서드로 키 "Three"에 해당하는 요소를 제거해 보겠습니다.
using System;
using System.Collections;
public class Demo {
public static void Main(String[] args) {
SortedList sortedList = new SortedList();
sortedList.Add("One", "Mouse");
sortedList.Add("Two", "Keyboard");
sortedList.Add("Three", "Headphone");
sortedList.Add("Four", "Speakers");
sortedList.Add("Five", "RAM");
Console.WriteLine("SortedList elements...");
foreach(DictionaryEntry d in sortedList) {
Console.WriteLine("Key = "+d.Key + ", Value = " + d.Value);
}
Console.WriteLine("Count of SortedList key-value pairs = "+sortedList.Count);
sortedList.Remove("Three");
Console.WriteLine("\nEnumerator to iterate through the SortedList...");
IDictionaryEnumerator demoEnum = sortedList.GetEnumerator();
while (demoEnum.MoveNext())
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
Console.WriteLine("Count of SortedList key-value pairs (Updated) = "+sortedList.Count);
}
}
출력 결과
위 프로그램을 실행하면 다음과 같은 결과가 출력됩니다.
SortedList elements... Key = Five, Value = RAM Key = Four, Value = Speakers Key = One, Value = Mouse Key = Three, Value = Headphone Key = Two, Value = Keyboard Count of SortedList key-value pairs = 5 Enumerator to iterate through the SortedList... Key = Five, Value = RAM Key = Four, Value = Speakers Key = One, Value = Mouse Key = Two, Value = Keyboard Count of SortedList key-value pairs (Updated) = 4
키 "Three"를 가진 요소(Headphone)가 성공적으로 제거되어 전체 요소 개수가 5개에서 4개로 변경되었습니다. SortedList는 키를 기준으로 항상 자동 정렬되므로, 요소를 제거한 후에도 나머지 요소들은 여전히 키 순서대로 정렬된 상태를 유지한다는 점도 함께 기억해 두면 좋습니다.