IReadOnlyCollection Interface Usage

This short program shows how read-only collections can be used. There have been times where I’ve wanted to expose the elements of a collection without giving the ability to change any elements. Just view them. From there if the collection needs to be modified it will have to be cast as another variable.

Granted it’s that way in most cases. I was messing around and was taught a way to modify the collection by way of casting an array object. I’ll demonstrate in the code.

ReadOnlyCollectionProgram ReadOnlyCollectionSolution

Program.cs code contents

using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Count
    class Program
        /// <summary>
        /// Demonstrating some usage around collections with IsReadOnly.
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
            string[] daysOfWeek = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };



        private static void basicUsage(string[] daysOfWeek)
            // Putting the daysOfWeek array into an ICollection variable.
            ICollection<string> collection = (ICollection<string>)daysOfWeek;

            // Checking to see if the collection is read-only and attempting to add a day.
            if (!collection.IsReadOnly)
                collection.Add("Slave Day");
                Console.WriteLine("Collection Is Read-Only");

            // Even though the collection is read-only you can cast the array collection and replace an element.
            (collection as string[])[5] = "SlaveDay";

            foreach (string day in collection)

        private static void createReadOnlyICollection()
            // Creating a collection to put into a read-only collection.
            ICollection<string> collection = new List<string>()
                "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"

            // Casting the collection as a read-only collection.
            IReadOnlyCollection<string> readOnlyCollection = (IReadOnlyCollection<string>)collection;

            Console.WriteLine("****Read-Only Collection Contents****");
            foreach (string days in readOnlyCollection)

            // You can also enumerate thru the collection to get the elements.
            Console.WriteLine("****Displaying Read-Only Contents With GetEnumerator****");
            List<string>.Enumerator testEnumeration = (List<string>.Enumerator)readOnlyCollection.GetEnumerator();
            while (testEnumeration.MoveNext())
                var value = testEnumeration.Current;

The basic usage of a read-only collection is demonstrated from the createReadOnlyICollection() method call. In the method being called you see that an ICollection interface of string type is being created and populated with days of the week as elements. Next I’m casting the ICollection to a IReadOnlyCollection of string type. Remember you’re not supposed to have the ability to modify a read-only collection once it’s created. So a cast needs to occur. Then you can iterate thru the collection to display the elements as being displayed with the foreach loop. You can also use GetEnumerator on a read-only collection to retrieve the elements.

Another usage of a read-only collection is being demonstrated by the basicUsage() method call. Here I’m taking the contents of an array and casting it into a read-only ICollection interface collection. To verify that this collection is read-only there’s a if/else statement with some simple logic checking the IsReadOnly state. I’ve been learning a lot about casting lately so as a cheat I decided to cast the read-only collection as a string array inline and modify the element to replace an element.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s