Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A Simple Immutable Hashtable for C#

0.00/5 (No votes)
21 Nov 2007 1  
A Hashtable wrapper class that permits the table to be filled with data in an early phase of a program, but then made read-only (Immutable) for subsequent usage.

Introduction

A software developer occasionally may have need for a container class, such as a Hashtable, that can be filled with data in an early phase of a program, but then made read-only (Immutable) for subsequent usage. This cannot be done with properties or member object access modifiers alone. Even if some class provides read-only access to its Hashtable member object, the contents of the Hashtable can still be changed, even if the Hashtable itself cannot be changed.

An Immutable Hashtable is a hybrid Hashtable collection in which:

- The internal member Hashtable is never directly accessible to the user, to prevent them from modifying its contents outside of the interface.

- Items may only be added to the collection via the Add () method, and only until the SetReadOnly () method is called. Once that method is invoked, the Hashtable is immutable.

- Immutability is enforced both by exception throwing and by Debug.Assert tests (since this is a design issue) when the program is run in the debug mode. So if a user attempts to call a method such as Add () after the p_isReadOnlyFlag is true, the ImmutableHash object throws either an assertion error when running in Debug mode, or a InvalidOperationException in Release mode.

Background

.NET does not provide users the ability to make a Hashtable read-only (or to make a read-only copy of a Hashtable, as is the case with ArrayList containers).

Using the code

The ImmutableHash class file can be added directly to a project, or to a dll library project. Users are responsible to change the namespace to something appropriate for their project.

using System;
using System.Threading;

namespace ImmutableHashTest
{
  class Program
  {
    static void Main (string[] args)
    {
      ImmutableHash iht = new ImmutableHash ();

      /// Objects can be added to the ImmutableHash

      /// iht until it is made read-only:


      iht.Add ("apples", 2);
      iht.Add ("oranges", 5);
      iht.Add ("peaches", 4);

      iht.SetReadOnly (); /// Make iht Immutable


      try
      {
        iht.Add ("plums", 6); /// will throw an exception

      }
      catch
      {
        Console.WriteLine ("could not add 'plums' to iht");

        Thread.Sleep (5000);
      }
    }
  }
}

Points of Interest

To make this class easier to work with in existing applications, the user can create a regular Hashtable object first, and use all the Hashtable methods and properties of the created object; then pass this object to the ImmutableHash when the ImmutableHash is created. Once the SetReadOnly () method is called, the hashtable object cannot be further modified from the ImmutableHash interface.

Other methods native to the inner hashtable can be exposed in the ImmutableHash interface if desired. However, it is important to understand that one should never return the inner hashtable object itself, as this would defeat the purpose of this class.

History

Initial version.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here