
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Arithmetic Operators
- C# - Assignment Operators
- C# - Relational Operators
- C# - Logical Operators
- C# - Bitwise Operators
- C# - Miscellaneous Operators
- C# - Operators Precedence
- C# Conditional Statements
- C# - Decision Making
- C# - If
- C# - If Else
- C# - Nested If
- C# - Switch
- C# - Nested Switch
- C# Control Statements
- C# - Loops
- C# - For Loop
- C# - While Loop
- C# - Do While Loop
- C# - Nested Loops
- C# - Break
- C# - Continue
- C# OOP & Data Handling
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
C# ArrayList - ReadOnly() Method
The C# ArrayList ReadOnly() method is used to create a read-only wrapper around an existing ArrayList.
This method makes the ArrayList immutable by making sure that elements cannot be added, removed, or modified. This method takes O(n) operation.
Syntax
Following is the syntax of the C# ArrayList ReadOnly() method −
public static ArrayList ReadOnly (ArrayList list);
Parameters
This method accepts an ArrayList that needs to be wrapped in read-only.
Return value
This method returns a read-only ArrayList wrapper that prevents modifications.
Example 1: Demonstrate the Use of ReadOnly() Method
Following is the basic example of the ReadOnly() method to demonstrate how to use it−
using System; using System.Collections; class Example { public static void Main() { // create and initialize new ArrayList ArrayList mylist = new ArrayList {1, 2, 3, 4, 5}; // creating a read-only wrapper ArrayList readonlList = ArrayList.ReadOnly(mylist); // Display the elements Console.WriteLine("Read Only List: "); foreach(var items in readonlList) Console.Write(items + " "); } }
Output
Following is the output −
Read Only List: 1 2 3 4 5
Example 2: Modifying Read-Only ArrayList in C#
Here, in this example, we create an ArrayList. We then use the ReadOnly() method to wrap in read-only. Then we attempt the modifications −
using System; using System.Collections; class Example { public static void Main() { // create and initialize new ArrayList ArrayList mylist = new ArrayList {1, 2, 3, 4, 5}; // creating a read-only wrapper ArrayList readonlList = ArrayList.ReadOnly(mylist); // Display the elements Console.WriteLine("Read Only List: "); foreach(var items in readonlList) Console.Write(items + " "); Console.Write("\n"); // Trying to modify the read-only list try { readonlList.Add(6); } catch (NotSupportedException e) { Console.WriteLine("Exception: " + e.Message); } } }
Output
Following is the output −
Read Only List: 1 2 3 4 5 Exception: Collection is read-only.
Example 3: Updating Read-Only ArrayList in C#
Here, in this example, we create an ArrayList. We then use the ReadOnly() method to wrap in read-only. Then, we attempt to update the elements of ArrayList −
using System; using System.Collections; class Example { public static void Main() { // create and initialize new ArrayList ArrayList mylist = new ArrayList {1, 2, 3, 4, 5}; // creating a read-only wrapper ArrayList readonlList = ArrayList.ReadOnly(mylist); // Display the elements Console.WriteLine("Read Only List: "); foreach(var items in readonlList){ Console.Write(items + " "); } Console.Write("\n"); try { readonlList[2] = 10; } catch (NotSupportedException e) { Console.WriteLine("Exception: " + e.Message); } } }
Output
Following is the output −
Read Only List: 1 2 3 4 5 Exception: Collection is read-only.
Example 4: Read-Only Wrapper with Dynamic Updates in List
This example demonstrates that the read-only ArrayList does not allow modifications; however, any changes made to the original ArrayList are still reflected in the read-only version −
using System; using System.Collections; class Example { static void Main() { ArrayList list = new ArrayList() { "Apple", "Banana", "Cherry" }; ArrayList readOnlyList = ArrayList.ReadOnly(list); Console.WriteLine("Read-Only List:"); foreach(var item in readOnlyList) { Console.Write(item + " "); } Console.WriteLine(); // trying to modify the read-only list try { readOnlyList.Add("Mango"); } catch (NotSupportedException e) { Console.WriteLine("Exception: " + e.Message); } // Modifying the original list // Allowed because myList is not read-only list.Add("Mango"); Console.WriteLine("Updated Original List:"); foreach(var item in list) { Console.Write(item + " "); } Console.WriteLine(); Console.WriteLine("Updated Read-Only List (Reflects changes in original):"); foreach(var item in readOnlyList) { Console.Write(item + " "); } } }
Output
Following is the output −
Read-Only List: Apple Banana Cherry Exception: Collection is read-only. Updated Original List: Apple Banana Cherry Mango Updated Read-Only List (Reflects changes in original): Apple Banana Cherry Mango