c# - How to make an ArrayList read-only

Make an ArrayList read-only
The ArrayList class implements the IList interface using an array whose size is dynamically increased as required. It is designed to hold heterogeneous collections of objects. ArrayList is not guaranteed to be sorted. The ArrayList capacity is the number of elements it can hold. Its capacity is automatically increased while adding elements. ArrayList elements can be accessed by index and it is zero-based. The ArrayList accepts null as a valid value and also allows duplicate elements.

The following .net c# tutorial code demonstrates how we can make an ArrayList read-only. That means we will convert an ArrayList instance to a read-only ArrayList. We also check whether an ArrayList instance is read-only or not. Here we will use the ArrayList class ReadOnly() method to make an ArrayList read-only. We also use the ArrayList class IsReadOnly property to determine whether an ArrayList is read-only or not.

A collection that is read-only is simply a collection with a wrapper that prevents modifying the collection. If changes are made to the underlying collection, the read-only collection reflects those changes. A collection that is read-only does not allow the addition, removal, or modification of elements after the collection is created.

The ArrayList ReadOnly() method returns a list wrapper that is read-only. The ArrayList ReadOnly(ArrayList) method overload returns a read-only ArrayList wrapper. The ArrayList ReadOnly(System.Collections.ArrayList list) method overload has a parameter named list. The list parameter is the ArrayList to wrap. This method returns a read-only ArrayList wrapper around the list. The method throws ArgumentNullException if the list is null.

The ArrayList class IsReadOnly property gets a value indicating whether the ArrayList is read-only. The property returns true if the ArrayList is read-only otherwise it returns false. The default value is false.

<%@ Page Language="C#" AutoEventWireup="true" %>

<!DOCTYPE html>

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
        ArrayList colors = new ArrayList() { "Salmon", "SandyBrown", "Gray" };

        Label1.Text = "<font color=DodgerBlue>'colors' ArrayList Elements... ";
        foreach (string color in colors)
            Label1.Text += "<br />" + color;
        Label1.Text += "</font>";

        ArrayList readOnlyColors = ArrayList.ReadOnly(colors);

        Label1.Text += "<br /><br />Is 'colors' ArrayList ReadOnly? " + colors.IsReadOnly.ToString();
        Label1.Text += "<br />Is 'readOnlyColors' ArrayList ReadOnly? " + readOnlyColors.IsReadOnly.ToString();


        //Uncomment this line to get error message.

        Label1.Text += "<br /><br /><font color=DodgerBlue>After Adding 'White', Now 'colors' ArrayList Elements... ";
        foreach (string color in colors)
            Label1.Text += "<br />" + color;
        Label1.Text += "</font>";

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to create a read-only copy of the ArrayList</title>
    <form id="form1" runat="server">
        <h2 style="color:MidnightBlue; font-style:italic;">
            System.Collections.ArrayList IsReadOnly Property
            <br /> How to create a read-only copy of the ArrayList
        <hr width="525" align="left" color="Navy" />
        <br />
             Font-Names="Courier New"
        <br /><br />
            Text="Test ArrayList IsReadOnly Property"
More asp.net examples