Open In App

C# | Remove from the specified index of 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.RemoveAt(Int32) method is used to remove the element at the specified index of a SortedList object.

Properties:



Syntax:

public virtual void RemoveAt (int index);

Here, index is the zero-based index 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 at
// the specified index of 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("DS", "Data Structures");
        mySortedList.Add("EE", "Electrical Engineering");
        mySortedList.Add("CS", "Computer Science");
        mySortedList.Add("ME", "Mechanical Engineering");
        mySortedList.Add("CE", "Civil Engineering");
  
        // 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 at index 4
        Console.WriteLine("Removing element at index 4");
  
        mySortedList.RemoveAt(4);
  
        // 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 = CE
Key = CS
Key = DS
Key = EE
Key = ME
Key = Civil Engineering
Key = Computer Science
Key = Data Structures
Key = Electrical Engineering
Key = Mechanical Engineering
Removing element at index 4
Key = CE
Key = CS
Key = DS
Key = EE
Key = Civil Engineering
Key = Computer Science
Key = Data Structures
Key = Electrical Engineering

Example 2:




// C# code to remove the element at
// the specified index of 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("DS", "Data Structures");
        mySortedList.Add("EE", "Electrical Engineering");
        mySortedList.Add("CS", "Computer Science");
        mySortedList.Add("ME", "Mechanical Engineering");
        mySortedList.Add("CE", "Civil Engineering");
  
        // 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 at index 8
        Console.WriteLine("Removing element at index 8");
  
        // It should raise ArgumentOutOfRangeException
        // As index is outside the range of valid
        // indexes for the SortedList object.
        mySortedList.RemoveAt(8);
  
        // 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.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

Note:

Reference:


Article Tags :
C#