Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » Generics » Downloads
 
Add your own
alternative version

Immutable Generic Collection Extensions

, 18 Dec 2007 CPOL
Create immutable / read-only collections from existing ICollection, IList, or IDictionary instances.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Com.WickedByte.Collections.Generic
{
    /// <summary>
    /// A generic read only list.
    /// </summary>
    /// <typeparam name="T">Type of data contained
    /// in the list.</typeparam>
    internal class ReadOnlyList<T> 
        : ReadOnlyCollection<T>, IList<T>
    {
        #region Private

        private readonly IList<T> list;

        #endregion

        #region IList<T> Members

        public int IndexOf( T item )
        {
            return list.IndexOf( item );
        }

        public void Insert( int index, T item )
        {
            throw new ReadOnlyException();
        }

        public void RemoveAt( int index )
        {
            throw new ReadOnlyException();
        }

        public T this[int index]
        {
            get
            {
                return list[index];
            }
            set
            {
                throw new ReadOnlyException();
            }
        }

        #endregion

        #region IEnumerable Members

        public new System.Collections.IEnumerator GetEnumerator()
        {
            return list.GetEnumerator();
        }

        #endregion

        /// <summary>
        /// Creates a new instance of a <see cref="ReadOnlyList"/>
        /// </summary>
        /// <param name="list">The mutable list that will
        /// be wrapped.</param>
        public ReadOnlyList( IList<T> list )
            : base( list )
        {
            this.list = list;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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

Share

About the Author

Marshall Rosenstein
Software Developer (Senior) WickedByte Software
United States United States
Marshall's torrid relationship with programming started as a child using BASIC on a Commodore PET computer in the 70's. He continued programming through high school, but did not study Computer Science in college. At the time, compilers would fail without telling you why, so after much soul searching, he realized he didn't want to make a living by spending eight hours a day looking for a missing semi-colon.
 
By the time he was pursuing his Ph.D. in Communication and Marketing, Microsoft had released Visual Studio. The improvements in the IDE were enough to cause Marshall to have late night affairs with COM and ASP. Marshall spent the dotcom bubble years as a web developer. After the bubble burst, he worked independently as a Java developer for medical applications. When Microsoft released an early beta of the .NET Framework, he was convinced to switch his focus from the Java Platform to the new Framework. He spent some time at Philips Medical Systems writing the data-access layer for the Carevue Chart hospital system. He is currently Technical Director for ASE Technologies.
 
Marshall lives in Salem, Massachusetts but would rather be in Hawaii.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 18 Dec 2007
Article Copyright 2007 by Marshall Rosenstein
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid