What is the IsReadOnly property of SortedList class in C#?

The IsReadOnly property of the SortedList class in C# returns a boolean value indicating whether the collection is read-only. A read-only collection cannot be modified after creation − you cannot add, remove, or update elements.

For standard SortedList instances created using the default constructor, this property always returns false, meaning the collection is modifiable.

Syntax

Following is the syntax for using the IsReadOnly property −

bool isReadOnly = sortedList.IsReadOnly;

Return Value

The IsReadOnly property returns −

  • true − if the SortedList is read-only

  • false − if the SortedList can be modified

Using IsReadOnly with Standard SortedList

Here's an example demonstrating the IsReadOnly property with a regular SortedList −

using System;
using System.Collections;

class Program {
    static void Main(string[] args) {
        SortedList s = new SortedList();
        s.Add("S001", "Jack");
        s.Add("S002", "Henry");
        s.Add("S003", "Alice");
        
        Console.WriteLine("IsReadOnly = " + s.IsReadOnly);
        Console.WriteLine("Count = " + s.Count);
        
        // Since it's not read-only, we can modify it
        s.Add("S004", "Bob");
        Console.WriteLine("After adding element, Count = " + s.Count);
    }
}

The output of the above code is −

IsReadOnly = False
Count = 3
After adding element, Count = 4

Using IsReadOnly with ReadOnly Wrapper

You can create a read-only wrapper using SortedList.ReadOnly() method to demonstrate when IsReadOnly returns true

using System;
using System.Collections;

class Program {
    static void Main(string[] args) {
        SortedList originalList = new SortedList();
        originalList.Add("A001", "John");
        originalList.Add("A002", "Mary");
        
        // Create read-only wrapper
        SortedList readOnlyList = SortedList.ReadOnly(originalList);
        
        Console.WriteLine("Original List IsReadOnly = " + originalList.IsReadOnly);
        Console.WriteLine("ReadOnly Wrapper IsReadOnly = " + readOnlyList.IsReadOnly);
        Console.WriteLine("Both lists have same count: " + readOnlyList.Count);
    }
}

The output of the above code is −

Original List IsReadOnly = False
ReadOnly Wrapper IsReadOnly = True
Both lists have same count: 2

Common Use Cases

The IsReadOnly property is typically used in scenarios where you need to −

  • Check if modifications are allowed before attempting to add/remove elements

  • Validate collection state in methods that expect modifiable collections

  • Implement defensive programming practices when working with collections of unknown origin

Conclusion

The IsReadOnly property provides a way to determine whether a SortedList can be modified. Standard SortedList instances return false, while read-only wrappers return true, helping you write safer code when working with collections.

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

163 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements