Click here to Skip to main content
6,630,586 members and growing! (16,913 online)
Email Password   helpLost your password?
Languages » C# » General     Intermediate

A Simple Immutable Hashtable for C#

By bearvarine

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.
C# 1.0, C# 2.0, C# 3.0, Windows, .NET 1.0, .NET 1.1, .NET 2.0, .NET 3.0, .NET 3.5, ASP.NET, WinForms, WebForms, VS.NET2003, VS2005, VS2008, Dev
Posted:21 Nov 2007
Views:7,525
Bookmarked:7 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
2 votes for this article.
Popularity: 0.48 Rating: 1.60 out of 5
1 vote, 50.0%
1

2

3
1 vote, 50.0%
4

5

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

About the Author

bearvarine


Member
Chief of Product Development, Panda Dental Software
Scientific Programmer for NOAA Fisheries, Seattle WA
Senior Software Engineer, Avaya
Occupation: Software Developer (Senior)
Company: Desktop Technical Services, WA
Location: United States United States

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 1 of 1 (Total in Forum: 1) (Refresh)FirstPrevNext
Generalconsider ReadOnlyCollection PinmemberKevin C Ferron22:07 21 Nov '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 21 Nov 2007
Editor:
Copyright 2007 by bearvarine
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project