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

The IsReadOnly property of the Hashtable class in C# returns a boolean value indicating whether the Hashtable is read-only. When a Hashtable is read-only, you cannot add, remove, or modify its elements.

By default, all Hashtable instances created using the standard constructor are not read-only, meaning they allow modifications. However, you can create read-only wrappers using specific methods.

Syntax

Following is the syntax for accessing the IsReadOnly property −

bool isReadOnly = hashtable.IsReadOnly;

Return Value

The IsReadOnly property returns −

  • true if the Hashtable is read-only
  • false if the Hashtable allows modifications

Example with Regular Hashtable

The following example demonstrates checking the IsReadOnly property of a standard Hashtable −

using System;
using System.Collections;

class Program {
    static void Main(string[] args) {
        Hashtable ht = new Hashtable();
        ht.Add("One", "Amit");
        ht.Add("Two", "Aman");
        ht.Add("Three", "Raman");
        
        Console.WriteLine("IsReadOnly = " + ht.IsReadOnly);
        Console.WriteLine("Can modify: " + !ht.IsReadOnly);
        
        // Demonstrate modification is allowed
        ht["Four"] = "Suman";
        Console.WriteLine("Added new element successfully");
        Console.WriteLine("Total elements: " + ht.Count);
    }
}

The output of the above code is −

IsReadOnly = False
Can modify: True
Added new element successfully
Total elements: 4

Example with Read-Only Hashtable

You can create a read-only wrapper of a Hashtable using the Hashtable.Synchronized method or by creating a custom read-only implementation −

using System;
using System.Collections;

class Program {
    static void Main(string[] args) {
        // Create a regular hashtable
        Hashtable originalHt = new Hashtable();
        originalHt.Add("A", "Apple");
        originalHt.Add("B", "Banana");
        
        Console.WriteLine("Original Hashtable IsReadOnly: " + originalHt.IsReadOnly);
        
        // Create a synchronized version (still not read-only)
        Hashtable syncHt = Hashtable.Synchronized(originalHt);
        Console.WriteLine("Synchronized Hashtable IsReadOnly: " + syncHt.IsReadOnly);
        
        // Both allow modifications
        originalHt["C"] = "Cherry";
        syncHt["D"] = "Date";
        
        Console.WriteLine("Original Count: " + originalHt.Count);
        Console.WriteLine("Synchronized Count: " + syncHt.Count);
    }
}

The output of the above code is −

Original Hashtable IsReadOnly: False
Synchronized Hashtable IsReadOnly: False
Original Count: 4
Synchronized Count: 4

Common Use Cases

The IsReadOnly property is useful when −

  • You need to verify if a Hashtable can be modified before attempting operations

  • You're working with Hashtables from different sources and need to check their mutability

  • You want to implement defensive programming practices by checking permissions before modifications

Example with Conditional Modification

using System;
using System.Collections;

class Program {
    static void SafeAdd(Hashtable ht, object key, object value) {
        if (ht.IsReadOnly) {
            Console.WriteLine("Cannot add - Hashtable is read-only");
        } else {
            ht.Add(key, value);
            Console.WriteLine($"Added {key}: {value}");
        }
    }
    
    static void Main(string[] args) {
        Hashtable ht = new Hashtable();
        ht.Add("Initial", "Value");
        
        Console.WriteLine("Hashtable IsReadOnly: " + ht.IsReadOnly);
        SafeAdd(ht, "New", "Element");
        
        Console.WriteLine("Final count: " + ht.Count);
    }
}

The output of the above code is −

Hashtable IsReadOnly: False
Added New: Element
Final count: 2

Conclusion

The IsReadOnly property of the Hashtable class provides a way to check whether the collection allows modifications. While standard Hashtables return false for this property, it's essential for ensuring safe operations when working with collections from various sources.

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

174 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements