More Read-Only Examples

Another couple of ways that you can make a collection read-only is by applying a AsReadOnly() method or by using ReadOnlyCollection. This is just another way that you can expose collection details. For example if you want to expose the element details to another layer of the program for display purposes only. This is one way you can do it.

AsReadOnlyProgram AsReadOnlySolution

Program.cs code contents

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

namespace Lists
    class Program
        /// <summary>
        /// Demonstrating examples of using a read-only collections.
        /// </summary>
        /// <param name="args"></param>

        static void Main(string[] args)


        // Method that creates a default list. Notice the count before and after and how it automatically allocates more space when the
        // list capacity limit is gone over.
        private static void createDynamicList()
            List<string> presidents = new List<string>()
                "Jimmy Carter",
                "Ronald Reagan",
                "George HW Bush",
                "Bill Clinton",
                "George W Bush"


        private static void readOnlyList(List<string> list)
            Console.WriteLine("****Displaying Read-Only List*****");

            // To keep the badCode method from doing anything to your list you can do a couple of things.
            // You can copy the list. But if the list is big you might take a performance hit.
            // Or you can call the AsReadOnly() method like this.
            var readOnlyList = list.AsReadOnly();

            // Or you can create the read-only list this way.
            var copy = new ReadOnlyCollection<string>(list);

            // Then an exception will be thrown if the method tries to modify the list.
            // I've added a try catch to display an error.
            catch (Exception ex)
                Console.WriteLine("Error: {0}", ex.Message);

            foreach (string president in list)

        // This is a method that I'm pretending that I don't know what it does when called upon.
        private static void badCode(IList<string> list)

Here you see me calling a few methods that create a string List then apply a method call and an object call. Both of these calls take the list and turn it into a read-only collection.

The first call is to the AsReadOnly() method. The next call is to the ReadOnlyCollection class. And for testing I’ve created a simple try/catch statement that runs a method that attempts to remove an element from the read-only list. Then catching the exception and displaying the message as you can see from the screenshot above.

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