Click here to Skip to main content
15,881,139 members
Articles / Web Development / ASP.NET
Article

A Simple Immutable ArrayList for C#

Rate me:
Please Sign up or sign in to vote.
2.44/5 (9 votes)
21 Nov 2007CPOL2 min read 39.1K   12   8
An ArrayList container 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 an ArrayList, 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 ArrayList member object, the contents of the ArrayList can still be changed, even if the ArrayList itself cannot be changed.

An Immutable ArrayList is a hybrid ArrayList container in which:

- The internal member ArrayList is never directly accessible to the users, 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 ArrayList 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 ImmutableArrayList object throws either an assertion error when running in Debug mode, or a InvalidOperationException in Release mode.

Download ImmutableArrayListTest.zip - 8.3 KB

Background

.NET provides the ability to make a read-only COPY of an ArrayList with the following construct:

ArrayList myAL = new ArrayList ();
...
ArrayList myReadOnlyAL = ArrayList.ReadOnly (myAL)

But now you have two objects to manage and you had to pay for making a new copy of an already existing Container. With an ImmutableArrayList class, you still only have one container object as you are not making any extra copies of it. The ImmutableArrayList class essentially functions as a read-only interface wrapper for an ArrayList. Plus, you can tweak its interface to suit your needs (maybe you want to make the ReadOnly flag reversable under certain conditions).

Using the code

The ImmutableArrayList 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 ImmutableArrayListTest
{
  class Program
  {
    static void Main (string[] args)
    {
      ImmutableArrayList ial = new ImmutableArrayList ();

      /// Objects can be added to the ImmutableArrayList
      /// iht until it is made read-only:

      ial.Add ("apples");
      ial.Add ("oranges");
      ial.Add ("peaches");

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

      try
      {
        ial.Add ("plums"); /// will throw an exception
      }
      catch
      {
        Console.WriteLine ("could not add 'plums' to ial");

        Thread.Sleep (5000);
      }
    }
  }
}

Points of Interest

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

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

History

Initial version.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Desktop Technical Services, WA
United States United States
Chief of Product Development, Panda Dental Software
Scientific Programmer for NOAA Fisheries, Seattle WA
Senior Software Engineer, Avaya

Comments and Discussions

 
GeneralMake array list immutable from construction Pin
Memeoid22-Nov-07 4:20
Memeoid22-Nov-07 4:20 
GeneralRe: Make array list immutable from construction Pin
Axel Rietschin23-Nov-07 3:59
professionalAxel Rietschin23-Nov-07 3:59 
AnswerRe: Make array list immutable from construction Pin
bearvarine23-Nov-07 9:31
bearvarine23-Nov-07 9:31 
QuestionWhat is the reason not to use the existing functionality? Pin
Mykola Tarasyuk22-Nov-07 4:16
Mykola Tarasyuk22-Nov-07 4:16 
AnswerRe: What is the reason not to use the existing functionality? [modified] Pin
bearvarine23-Nov-07 9:25
bearvarine23-Nov-07 9:25 
QuestionSource code? Pin
Jcmorin22-Nov-07 1:59
Jcmorin22-Nov-07 1:59 
AnswerRe: Source code? Pin
bearvarine23-Nov-07 9:19
bearvarine23-Nov-07 9:19 
QuestionWhy an ArrayList? Pin
homerbush21-Nov-07 18:47
homerbush21-Nov-07 18:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.