C# | Replacing the value at a specific index in a SortedList object Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report 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: CSharp // 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 CSharp // 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: 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: https://learn.microsoft.com/en-us/dotnet/api/system.collections.sortedlist.setbyindex?view=netframework-4.7.2 Comment More infoAdvertise with us K Kirti_Mangal Follow Improve Article Tags : C# CSharp-method CSharp-Collections-Namespace CSharp-Collections-SortedList Explore IntroductionC# Tutorial 4 min read Introduction to .NET Framework 6 min read C# .NET Framework (Basic Architecture and Component Stack) 6 min read C# Hello World 2 min read Common Language Runtime (CLR) in C# 4 min read FundamentalsC# Identifiers 2 min read Data Types in C# 6 min read C# Variables 4 min read C# Literals 5 min read Operators in C# 7 min read C# Keywords 5 min read Control StatementsC# Decision Making (if, if-else, if-else-if ladder, nested if, switch, nested switch) 5 min read C# Switch Statement 4 min read Loops in C# 4 min read C# Jump Statements (Break, Continue, Goto, Return and Throw) 4 min read OOP ConceptsC# Class and Objects 5 min read Constructors in C# 5 min read C# Inheritance 3 min read C# Encapsulation 4 min read C# Abstraction 4 min read MethodsMethods in C# 4 min read Method Overloading in C# 4 min read C# | Method Parameters 7 min read C# Method Overriding 9 min read Anonymous Method in C# 3 min read ArraysArrays in C# 6 min read Jagged Arrays in C# 4 min read Array Class in C# 5 min read How to Sort an Array in C# | Array.Sort() Method Set - 1 8 min read How to find the rank of an array in C# 2 min read ArrayListArrayList in C# 6 min read C# ArrayList Class 7 min read C# | Array vs ArrayList 2 min read StringStrings in C# 6 min read C# Verbatim String Literal - @ 5 min read C# String Class 9 min read C# StringBuilder 4 min read C# String vs StringBuilder 3 min read TupleC# Tuple 7 min read C# Tuple Class 3 min read C# ValueTuple 7 min read C# ValueTuple Struct 4 min read IndexersC# Indexers 4 min read C# Multidimensional Indexers 5 min read C# - Overloading of Indexers 3 min read Like