Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » Generics » Downloads
 

Immutable Generic Collection Extensions

By , 18 Dec 2007
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Com.WickedByte.Collections.Generic
{
    /// <summary>
    /// A generic read-only collection.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    internal class ReadOnlyCollection<T> : ICollection<T>
    {
        #region Private

        private readonly ICollection<T> collection;

        #endregion

        #region ICollection<T> Members

        public void Add( T item )
        {
            throw new ReadOnlyException();
        }

        public void Clear()
        {
            throw new ReadOnlyException();
        }

        public bool Contains( T item )
        {
            return collection.Contains( item );
        }

        public void CopyTo( T[] array, int arrayIndex )
        {
            collection.CopyTo( array, arrayIndex );
        }

        public int Count
        {
            get { return collection.Count; }
        }

        public bool IsReadOnly
        {
            get { return collection.IsReadOnly; }
        }

        public bool Remove( T item )
        {
            throw new ReadOnlyException();
        }

        #endregion

        #region IEnumerable<T> Members

        public IEnumerator<T> GetEnumerator()
        {
            return collection.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return collection.GetEnumerator();
        }

        #endregion

        public ReadOnlyCollection( ICollection<T> collection )
        {
            this.collection = collection;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of use 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)

About the Author

Marshall Rosenstein
Software Developer (Senior) WickedByte Software
United States United States
Member
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.

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