Open In App

C# | Remove the element with the specified key from a SortedList

SortedList class is a collection of (key, value) pairs which are sorted according to keys. Those pairs can be accessible by key and as well as by index(zero-based indexing). This comes under System.Collections namespace. SortedList.Remove(Object) method is used to remove the element with the specified key from a SortedList object.

Properties:



Syntax :

public virtual void Remove (object key);

Here, key is the key of the element to remove.



Exceptions:

Below given are some examples to understand the implementation in a better way:

Example 1:




// C# code to remove the element
// with the specified key from a SortedList
using System;
using System.Collections;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating an SortedList
        SortedList mySortedList = new SortedList();
  
        // Adding elements to SortedList
        mySortedList.Add("IN", "INDIA");
        mySortedList.Add("NY", "NEW-YORK");
        mySortedList.Add("UK", "UNITED KINGDOM");
        mySortedList.Add("GER", "GERMANY");
        mySortedList.Add("CH", "CHINA");
  
        // Displaying elements in SortedList
        foreach (string mykey in mySortedList.Keys)
            Console.WriteLine("Key = " + mykey);
  
        foreach (string myvalue in mySortedList.Values)
            Console.WriteLine("Key = " + myvalue);
  
        // Removing element having key as "UK"
        Console.WriteLine("Removing element having key as UK");
  
        mySortedList.Remove("UK");
  
        // Displaying elements in SortedList
        foreach (string mykey in mySortedList.Keys)
            Console.WriteLine("Key = " + mykey);
  
        foreach (string myvalue in mySortedList.Values)
            Console.WriteLine("Key = " + myvalue);
    }
}

Output:
Key = CH
Key = GER
Key = IN
Key = NY
Key = UK
Key = CHINA
Key = GERMANY
Key = INDIA
Key = NEW-YORK
Key = UNITED KINGDOM
Removing element having key as UK
Key = CH
Key = GER
Key = IN
Key = NY
Key = CHINA
Key = GERMANY
Key = INDIA
Key = NEW-YORK

Example 2:




// C# code to remove the element
// with the specified key from a SortedList
using System;
using System.Collections;
using System.Collections.Generic;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating an SortedList
        SortedList mySortedList = new SortedList();
  
        // Adding elements to SortedList
        mySortedList.Add("IN", "INDIA");
        mySortedList.Add("NY", "NEW-YORK");
        mySortedList.Add("UK", "UNITED KINGDOM");
        mySortedList.Add("GER", "GERMANY");
        mySortedList.Add("CH", "CHINA");
  
        // Displaying elements in SortedList
        foreach (string mykey in mySortedList.Keys)
            Console.WriteLine("Key = " + mykey);
  
        foreach (string myvalue in mySortedList.Values)
            Console.WriteLine("Key = " + myvalue);
  
        // Removing element having key as null
        Console.WriteLine("Removing element having key as null");
  
        // It should raise ArgumentNullException
        // as key can not be null
        mySortedList.Remove(null);
  
        // Displaying elements in SortedList
        foreach (string mykey in mySortedList.Keys)
            Console.WriteLine("Key = " + mykey);
  
        foreach (string myvalue in mySortedList.Values)
            Console.WriteLine("Key = " + myvalue);
    }
}

Error:

Unhandled Exception:
System.ArgumentNullException: Key cannot be null.
Parameter name: key

Note:

Reference:


Article Tags :
C#