Check if a SortedList is read-only in C#

The SortedList class in C# provides the IsReadOnly property to determine whether the collection can be modified. A read-only SortedList cannot have elements added, removed, or modified after creation.

Syntax

Following is the syntax to check if a SortedList is read-only −

bool isReadOnly = sortedList.IsReadOnly;

Return Value

The IsReadOnly property returns a bool value −

  • true if the SortedList is read-only

  • false if the SortedList allows modifications

Example

The following example demonstrates how to check if a SortedList is read-only −

using System;
using System.Collections;

public class Demo {
   public static void Main(String[] args) {
      SortedList list = new SortedList();
      list.Add("One", "IT");
      list.Add("Two", "Operations");
      list.Add("Three", "Marketing");
      list.Add("Four", "Purchase");
      list.Add("Five", "Sales");
      list.Add("Six", "Finance");
      
      Console.WriteLine("SortedList elements...");
      foreach(DictionaryEntry d in list) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      
      Console.WriteLine("\nList of values...SortedList");
      IList col = list.GetValueList();
      foreach(string res in col) {
         Console.WriteLine(res);
      }
      
      Console.WriteLine("\nSortedList is read-only? = " + list.IsReadOnly);
   }
}

The output of the above code is −

SortedList elements...
Five Sales
Four Purchase
One IT
Six Finance
Three Marketing
Two Operations

List of values...SortedList
Sales
Purchase
IT
Finance
Marketing
Operations

SortedList is read-only? = False

Using SortedList Keys Collection

The following example shows checking the read-only property while working with keys collection −

using System;
using System.Collections;

public class Demo {
   public static void Main(String[] args) {
      SortedList list = new SortedList();
      list.Add("One", "Finance");
      list.Add("Two", "Marketing");
      list.Add("Three", "Sales");
      list.Add("Four", "Purchase");
      list.Add("Five", "Operations");
      list.Add("Six", "IT");
      
      Console.WriteLine("SortedList elements...");
      foreach(DictionaryEntry d in list) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      
      Console.WriteLine("\nIndex at key One = " + list.IndexOfKey("One"));
      
      ICollection col = list.Keys;
      Console.WriteLine("\nCollection of Keys...");
      foreach(string res in col)
         Console.WriteLine(res);
      
      Console.WriteLine("\nSortedList is read-only? = " + list.IsReadOnly);
   }
}

The output of the above code is −

SortedList elements...
Five Operations
Four Purchase
One Finance
Six IT
Three Sales
Two Marketing

Index at key One = 2

Collection of Keys...
Five
Four
One
Six
Three
Two

SortedList is read-only? = False

How It Works

By default, a newly created SortedList has IsReadOnly set to false, meaning it allows modifications. The elements are automatically sorted by their keys when added to the collection. The IsReadOnly property is particularly useful when you need to verify whether a SortedList can be safely modified in your application logic.

Conclusion

The IsReadOnly property provides a simple way to check if a SortedList can be modified. By default, SortedList collections are not read-only and allow adding, removing, and updating elements.

Updated on: 2026-03-17T07:04:36+05:30

136 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements