
.NET 2.0+A Generic Read-Only Dictionary
The .NET framework includes several types of collection that are designed for use in object models. Amongst these is the ReadOnlyCollection that allows the creation of collections that may not be modified. However, there is no read-only Dictionary type.
Object Model Collections
The .NET framework versions 2.0 and later include the System.Collections.ObjectModel namespace. This namespace contains various collection classes that are designed to be used when creating reusable object models, such as those held in a class library. These types should be the preferred choices when creating public methods and properties that return lists of values or objects.
ReadOnlyCollection Class
One of the interesting classes in the System.Collections.ObjectModel namespace is the ReadOnlyCollection. This class provides a wrapper to an existing collection but without the members that permit you to add, remove or modify items. This is useful when returning a list of values to a calling method where you do not wish for the collection to be changed. NB: The collection may not be modified but if it contains reference types, the individual items may be.
To create an instance of the ReadOnlyCollection class, you must first create another, writeable collection. This is passed to the constructor of the ReadOnlyCollection object, as in the following sample code:
Collection<string> writeable = new Collection<string>();
writeable.Add("One");
writeable.Add("Two");
writeable.Add("Three");
ReadOnlyCollection<string> readOnly = new ReadOnlyCollection<string>(writeable);
Unfortunately, the .NET framework does not provide a read-only dictionary or hash table class. In this article we will implement such a dictionary as a generic, "ReadOnlyDictionary" class.
ReadOnlyDictionary Requirements
The ReadOnlyDictionary class will behave in a similar manner to the generic ReadOnlyCollection class. The key difference will be that the class will be a wrapper for a dictionary containing key / value pairs, rather than for a simple list of values. The class will meet the following requirements:
- The dictionary will contain key / value pairs where both the key and value are generic types, permitting any type of key and value to be held.
- It will not be possible to add, remove or replace items in the collection. Changes to the individual items in the collection will be permitted if their types permit. These changes will be reflected in the underlying dictionary also.
- The ReadOnlyDictionary class will implement the generic IDictionary interface so that it may be used wherever an IDictionary object is expected. In order to implement the IDictionary interface, the ReadOnlyDictionary will also implement the generic ICollection and IEnumerable interfaces and the non-generic IEnumerable interface.
Creating the ReadOnlyDictionary Project
The ReadOnlyDictionary class described in this article can be created in a class library project or in any other type of C# project, depending upon your requirements. The code that can be downloaded from the link at the top of the page uses a class library project, named "ReadOnlyDictionaryDemo".
28 February 2009