C# | Replacing the value at a specific index in a SortedList object

SortedList.SetByIndex(Int32, Object) Method is used to replace the value at a specific index in a SortedList object.

Syntax:

public virtual void SetByIndex (int index, object value);

Parameters:

index: It is the zero-based index at which to save value.

value: It is the Object to save into the SortedList object. The value can be null.



Exception: This method throws ArgumentOutOfRangeException if the index is outside the range of the valid indexes of the given SortedList Object.

Below programs illustrate the use of above-discussed method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code for replacing the value at a
// specific index in a SortedList object
using System;
using System.Collections;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // Creating a SortedList of integers
        SortedList mylist = new SortedList();
  
        // Adding elements to SortedList
        mylist.Add("First", "Ram");
        mylist.Add("Second", "Shyam");
        mylist.Add("Third", "Mohit");
        mylist.Add("Fourth", "Rohit");
        mylist.Add("Fifth", "Manish");
  
        // Before replacing the keys
        // and values of SortedList are
        Console.WriteLine("----- Before Replacing -----");
  
        Console.WriteLine("Index \t\t Keys \t\tValues");
  
        for (int i = 0; i < mylist.Count; i++) {
            Console.WriteLine("[{0}]\t\t{1}\t\t{2}", i,
                              mylist.GetKey(i), mylist.GetByIndex(i));
        }
  
        Console.WriteLine();
  
        // After replacing the keys
        // and values of SortedList are
        Console.WriteLine("----- After Replacing -----");
  
        // Replaces the values at
        // index 1 and index 3.
        mylist.SetByIndex(1, "Priyanka");
        mylist.SetByIndex(3, "Ritu");
  
        Console.WriteLine("Index \t\t Keys \t\tValues");
  
        for (int i = 0; i < mylist.Count; i++) {
            Console.WriteLine("[{0}]\t\t{1}\t\t{2}", i,
                              mylist.GetKey(i), mylist.GetByIndex(i));
        }
    }
}

chevron_right


Output:

----- Before Replacing -----
Index          Keys         Values
[0]        Fifth        Manish
[1]        First        Ram
[2]        Fourth        Rohit
[3]        Second        Shyam
[4]        Third        Mohit

----- After Replacing -----
Index          Keys         Values
[0]        Fifth        Manish
[1]        First        Priyanka
[2]        Fourth        Rohit
[3]        Second        Ritu
[4]        Third        Mohit

Example 2: Demonstrating the case where ArgumentOutOfRangeException can occur

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code giving ArgumentOutOfRangeException 
// specific index in a SortedList object
// but giving ArgumentOutOfRangeException 
using System;
using System.Collections;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // Creating a SortedList of integers
        SortedList mylist = new SortedList();
  
        // Adding elements to SortedList
        mylist.Add("h", "Hello"); 
        mylist.Add("g", "Geeks!"); 
        mylist.Add("w", "Welcome");
        mylist.Add("t", "to");
        mylist.Add("n", "Noida"); 
  
        // Before replacing the keys
        // and values of SortedList are
        Console.WriteLine("----- Before Replacing -----");
  
        Console.WriteLine("Index \t\t Keys \t\tValues");
  
        for (int i = 0; i < mylist.Count; i++) {
            Console.WriteLine("[{0}]\t\t{1}\t\t{2}", i,
                            mylist.GetKey(i), mylist.GetByIndex(i));
        }
  
        Console.WriteLine();
  
        // After replacing the keys
        // and values of SortedList are
        Console.WriteLine("----- After Replacing -----");
  
        // Replaces the values at
        // index 6 which is outside 
        // the range of valid indexes
        // here it will give an
        // ArgumentOutOfRangeException
        mylist.SetByIndex(6, null);
  
        Console.WriteLine("Index \t\t Keys \t\tValues");
  
        for (int i = 0; i < mylist.Count; i++) {
            Console.WriteLine("[{0}]\t\t{1}\t\t{2}", i,
                            mylist.GetKey(i), mylist.GetByIndex(i));
        }
    }
}

chevron_right


Runtime 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:

  • The index sequence is based on the sort sequence. When an element is added, it is inserted into SortedList in the correct sort order, and the indexing adjusts accordingly. When an element is removed, the indexing also adjusts accordingly. So, the index of a specific key/value pair may change after adding and removing the elements from the SortedList object.
  • This method is an O(1) operation.

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.