Open In App

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:




// 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));
        }
    }
}

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




// 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));
        }
    }
}

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:

Reference:


Article Tags :
C#