C# में SortedList वर्ग की Remove(key) विधि का उपयोग करके निर्दिष्ट कुंजी और उससे जुड़े मान को हटाया जा सकता है। यह विधि कुंजी मिलने पर प्रविष्टि हटा देती है। SortedList कुंजियों के आधार पर स्वचालित रूप से क्रमबद्ध रहता है।
उदाहरण 1: कुंजी "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 तत्व...");
foreach(DictionaryEntry d in sortedList) {
Console.WriteLine("कुंजी = " + d.Key + ", मान = " + d.Value);
}
Console.WriteLine("SortedList कुंजी-मान जोड़ों की संख्या = " + sortedList.Count);
sortedList.Remove("H");
Console.WriteLine("\nSortedList के माध्यम से गणक...");
IDictionaryEnumerator demoEnum = sortedList.GetEnumerator();
while (demoEnum.MoveNext())
Console.WriteLine("कुंजी = " + demoEnum.Key + ", मान = " + demoEnum.Value);
Console.WriteLine("SortedList कुंजी-मान जोड़ों की संख्या (अपडेटेड) = " + sortedList.Count);
}
}
आउटपुट
SortedList तत्व... कुंजी = A, मान = 1 कुंजी = B, मान = 2 कुंजी = C, मान = 3 कुंजी = D, मान = 4 कुंजी = E, मान = 5 कुंजी = F, मान = 6 कुंजी = G, मान = 7 कुंजी = H, मान = 8 कुंजी = I, मान = 9 कुंजी = J, मान = 10 SortedList कुंजी-मान जोड़ों की संख्या = 10 SortedList के माध्यम से गणक... कुंजी = A, मान = 1 कुंजी = B, मान = 2 कुंजी = C, मान = 3 कुंजी = D, मान = 4 कुंजी = E, मान = 5 कुंजी = F, मान = 6 कुंजी = G, मान = 7 कुंजी = I, मान = 9 कुंजी = J, मान = 10 SortedList कुंजी-मान जोड़ों की संख्या (अपडेटेड) = 9
उदाहरण 2: कुंजी "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 तत्व...");
foreach(DictionaryEntry d in sortedList) {
Console.WriteLine("कुंजी = " + d.Key + ", मान = " + d.Value);
}
Console.WriteLine("SortedList कुंजी-मान जोड़ों की संख्या = " + sortedList.Count);
sortedList.Remove("Three");
Console.WriteLine("\nSortedList के माध्यम से गणक...");
IDictionaryEnumerator demoEnum = sortedList.GetEnumerator();
while (demoEnum.MoveNext())
Console.WriteLine("कुंजी = " + demoEnum.Key + ", मान = " + demoEnum.Value);
Console.WriteLine("SortedList कुंजी-मान जोड़ों की संख्या (अपडेटेड) = " + sortedList.Count);
}
}
आउटपुट
SortedList तत्व... कुंजी = Five, मान = RAM कुंजी = Four, मान = Speakers कुंजी = One, मान = Mouse कुंजी = Three, मान = Headphone कुंजी = Two, मान = Keyboard SortedList कुंजी-मान जोड़ों की संख्या = 5 SortedList के माध्यम से गणक... कुंजी = Five, मान = RAM कुंजी = Four, मान = Speakers कुंजी = One, मान = Mouse कुंजी = Two, मान = Keyboard SortedList कुंजी-मान जोड़ों की संख्या (अपडेटेड) = 4
मुख्य बिंदु
Remove(key)निर्दिष्ट कुंजी और उसका मान हटाता है- कुंजी नहीं मिलने पर कोई अपवाद नहीं फेंकता
RemoveAt(index)से सूचकांक द्वारा भी हटा सकते हैं- SortedList कुंजियों के आधार पर स्वचालित क्रमबद्ध क्रम बनाए रखता है
IDictionaryEnumeratorसे कुंजी-मान जोड़े पुनरावृत्त कर सकते हैं