Check if OrderedDictionary collection is read-only in C#

The OrderedDictionary class in C# provides a collection that maintains the order of key-value pairs while allowing access by both key and index. To check if an OrderedDictionary is read-only, you use the IsReadOnly property.

An OrderedDictionary is typically not read-only by default, meaning you can add, modify, and remove elements after creation. However, you can create a read-only wrapper using specific methods.

Syntax

Following is the syntax to check if an OrderedDictionary is read-only −

bool isReadOnly = orderedDictionary.IsReadOnly;

Following is the syntax to create a read-only wrapper −

OrderedDictionary readOnlyDict = OrderedDictionary.ReadOnly(orderedDictionary);

Using IsReadOnly Property

The IsReadOnly property returns false for a regular OrderedDictionary and true for a read-only wrapper −

Example

using System;
using System.Collections;
using System.Collections.Specialized;

public class Demo {
    public static void Main(){
        OrderedDictionary dict = new OrderedDictionary();
        dict.Add("1", "One");
        dict.Add("2", "Two");
        dict.Add("3", "Three");
        dict.Add("4", "Four");
        dict.Add("5", "Five");
        dict.Add("6", "Six");
        dict.Add("7", "Seven");
        dict.Add("8", "Eight");

        ICollection col = dict.Values;
        String[] strVal = new String[dict.Count];
        col.CopyTo(strVal, 0);

        Console.WriteLine("Displaying the values...");
        for (int i = 0; i < dict.Count; i++) {
            Console.WriteLine(strVal[i]);
        }

        Console.WriteLine("Is OrderedDictionary read-only? = " + dict.IsReadOnly);
        Console.WriteLine("The OrderedDictionary has the key 15? = " + dict.Contains("15"));
        Console.WriteLine("The OrderedDictionary has the key 5? = " + dict.Contains("5"));
    }
}

The output of the above code is −

Displaying the values...
One
Two
Three
Four
Five
Six
Seven
Eight
Is OrderedDictionary read-only? = False
The OrderedDictionary has the key 15? = False
The OrderedDictionary has the key 5? = True

Creating a Read-Only OrderedDictionary

You can create a read-only wrapper of an OrderedDictionary using the ReadOnly method −

Example

using System;
using System.Collections;
using System.Collections.Specialized;

public class Demo {
    public static void Main(){
        OrderedDictionary dict = new OrderedDictionary();
        dict.Add("A", "Pen");
        dict.Add("B", "Pencil");
        dict.Add("C", "Notebook");
        dict.Add("D", "Table");

        Console.WriteLine("Original OrderedDictionary IsReadOnly: " + dict.IsReadOnly);

        // Create a read-only wrapper
        OrderedDictionary readOnlyDict = OrderedDictionary.ReadOnly(dict);
        Console.WriteLine("Read-only wrapper IsReadOnly: " + readOnlyDict.IsReadOnly);

        // Display values from read-only dictionary
        Console.WriteLine("Values from read-only dictionary:");
        foreach (DictionaryEntry entry in readOnlyDict) {
            Console.WriteLine($"Key: {entry.Key}, Value: {entry.Value}");
        }

        Console.WriteLine("The read-only OrderedDictionary has the key B? = " + readOnlyDict.Contains("B"));
    }
}

The output of the above code is −

Original OrderedDictionary IsReadOnly: False
Read-only wrapper IsReadOnly: True
Values from read-only dictionary:
Key: A, Value: Pen
Key: B, Value: Pencil
Key: C, Value: Notebook
Key: D, Value: Table
The read-only OrderedDictionary has the key B? = True

Key Properties of OrderedDictionary

Property Description
IsReadOnly Returns false for regular dictionaries, true for read-only wrappers
Count Gets the number of key-value pairs in the dictionary
Keys Gets a collection containing the keys in the dictionary
Values Gets a collection containing the values in the dictionary

Conclusion

The IsReadOnly property of OrderedDictionary allows you to check if the collection can be modified. Regular instances return false, while read-only wrappers created with OrderedDictionary.ReadOnly() return true, preventing modifications to the collection.

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

148 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements