Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Get a read-only copy of the OrderedDictionary in C#
The OrderedDictionary class in C# provides the AsReadOnly() method to create a read-only copy of the collection. This wrapper prevents any modifications to the dictionary while maintaining access to all elements in their insertion order.
Syntax
Following is the syntax for creating a read-only copy of an OrderedDictionary −
OrderedDictionary readOnlyDict = originalDict.AsReadOnly();
Return Value
The AsReadOnly() method returns an OrderedDictionary wrapper that is read-only. Any attempt to modify this wrapper will throw a NotSupportedException.
Using AsReadOnly() Method
The following example demonstrates how to create a read-only copy and verify its read-only status −
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
OrderedDictionary dict = new OrderedDictionary();
dict.Add("A", "Books");
dict.Add("B", "Electronics");
dict.Add("C", "Smart Wearables");
dict.Add("D", "Pet Supplies");
dict.Add("E", "Clothing");
dict.Add("F", "Footwear");
Console.WriteLine("Original OrderedDictionary elements...");
foreach(DictionaryEntry d in dict) {
Console.WriteLine(d.Key + " " + d.Value);
}
OrderedDictionary readOnlyDict = dict.AsReadOnly();
Console.WriteLine("\nIs OrderedDictionary read-only? = " + readOnlyDict.IsReadOnly);
Console.WriteLine("Original dictionary read-only? = " + dict.IsReadOnly);
}
}
The output of the above code is −
Original OrderedDictionary elements... A Books B Electronics C Smart Wearables D Pet Supplies E Clothing F Footwear Is OrderedDictionary read-only? = True Original dictionary read-only? = False
Accessing Elements from Read-Only Dictionary
You can still access and iterate through elements in a read-only OrderedDictionary, but modification operations will throw exceptions −
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");
OrderedDictionary readOnlyDict = dict.AsReadOnly();
Console.WriteLine("Accessing elements from read-only dictionary:");
Console.WriteLine("Key '1': " + readOnlyDict["1"]);
Console.WriteLine("Key '2': " + readOnlyDict["2"]);
Console.WriteLine("Count: " + readOnlyDict.Count);
Console.WriteLine("\nIterating through read-only dictionary:");
foreach(DictionaryEntry entry in readOnlyDict) {
Console.WriteLine(entry.Key + " = " + entry.Value);
}
}
}
The output of the above code is −
Accessing elements from read-only dictionary: Key '1': One Key '2': Two Count: 3 Iterating through read-only dictionary: 1 = One 2 = Two 3 = Three
Key Characteristics
-
The
AsReadOnly()method returns a wrapper, not a copy. Changes to the original dictionary reflect in the read-only wrapper. -
The read-only wrapper preserves the insertion order of elements.
-
Attempting to modify the read-only dictionary throws a
NotSupportedException. -
You can still access elements by key or index and iterate through the collection.
Conclusion
The AsReadOnly() method provides a convenient way to create a read-only wrapper of an OrderedDictionary. This wrapper maintains element access and iteration capabilities while preventing any modifications, making it ideal for scenarios where you need to share dictionary data without allowing changes.
