Click here to Skip to main content
13,043,898 members (53,855 online)
Click here to Skip to main content
Articles » Languages » C# » Generics » Downloads


17 bookmarked
Posted 13 Dec 2007

Immutable Generic Collection Extensions

, 18 Dec 2007
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 set of extensions to <see cref="System.Collections.Generic"/>
    /// data structures that enable the creation of read-only
    /// references.
    /// </summary>
    /// <remarks>The original data structure can still be
    /// modified, so changes to the original reference will
    /// still be reflected in the read-only wrapper.</remarks>
    public static class ReadOnlyExtensions
        /// <summary>
        /// Creates a read-only reference to a generic collection.
        /// </summary>
        /// <typeparam name="T">The type of objects contained
        /// in the collection.</typeparam>
        /// <param name="collection">The collection for which
        /// a read-only wrapper will be created.</param>
        /// <returns>Read-only wrapper for the collection.</returns>
        public static ICollection<T> ToReadOnly<T>( this ICollection<T> collection )
            return new ReadOnlyCollection<T>( collection );

        /// <summary>
        /// Creates a read-only reference to a generic list.
        /// </summary>
        /// <typeparam name="T">The type of objects contained
        /// in the list.</typeparam>
        /// <param name="list">The list for which
        /// a read-only wrapper will be created.</param>
        /// <returns>Read-only wrapper for the list.</returns>
        public static IList<T> ToReadOnly<T>( this IList<T> list )
            return new ReadOnlyList<T>( list );

        /// <summary>
        /// Creates a read-only reference to a generic dictionary.
        /// </summary>
        /// <typeparam name="TKey">The type of keys
        /// held in the dictionary.</typeparam>
        /// <typeparam name="TValue">The type of values held
        /// in the dictionary.</typeparam>
        /// <param name="dictionary">The dictionary for which
        /// a read-only wrapper will be created.</param>
        /// <returns>Read-only wrapper to the dictionary.</returns>
        public static IDictionary<TKey, TValue> ToReadOnly<TKey, TValue>( this IDictionary<TKey, TValue> dictionary )
            return new ReadOnlyDictionary<TKey, TValue>( dictionary );

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.


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
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.

You may also be interested in...

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