C# | Creating a read-only wrapper for the ArrayList

ArrayList.ReadOnly(ArrayList) Method is used to get a read-only ArrayList wrapper.

Syntax:

public static System.Collections.ArrayList ReadOnly(System.Collections.ArrayList list);

Here, the list is the ArrayList which is to be wrapped.

Return Value: It returns a read-only ArrayList Wrapper around the list.



Exception: This method returns the ArgumentNullException if the list is null.

Below programs illustrate the use of above-discussed method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to create a read-only
// wrapper for the ArrayList
using System;
using System.Collections;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating an ArrayList
        ArrayList myList = new ArrayList();
  
        // Adding elements to ArrayList
        myList.Add("Geeks");
        myList.Add("for");
        myList.Add("Geeks");
        myList.Add("Noida");
        myList.Add("Geeks Classes");
        myList.Add("Delhi");
  
        // Creating a Read-Only packing
        // around the ArrayList
        ArrayList myList2 = ArrayList.ReadOnly(myList);
  
        // --------- Using IsReadOnly Property
  
        // print the status of both ArrayList
        Console.WriteLine("myList ArrayList is {0}.",
                    myList.IsReadOnly ? "read-only"
                                    "not read-only");
  
        Console.WriteLine("myList2 ArrayList is {0}.",
                    myList2.IsReadOnly ? "read-only"
                                     "not read-only");
    }
}

chevron_right


Output:

myList ArrayList is not read-only.
myList2 ArrayList is read-only.

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to create a read-only 
// wrapper for the ArrayList
using System; 
using System.Collections; 
  
class GFG { 
  
    // Driver code 
    public static void Main() 
    
  
        // Creating an ArrayList 
        ArrayList myList = new ArrayList(); 
  
        // Adding elements to ArrayList 
        myList.Add("C"); 
        myList.Add("C++"); 
        myList.Add("Java"); 
        myList.Add("C#"); 
        myList.Add("Python"); 
          
        Console.WriteLine("Before Wrapping: ");
          
        // Displaying the elements in the ArrayList 
        foreach(string str in myList) 
        
            Console.WriteLine(str); 
        
          
      
        // Creating a Read-Only packing 
        // around the ArrayList 
        ArrayList myList2 = ArrayList.ReadOnly(myList);
          
        Console.WriteLine("After Wrapping: ");
          
        // Displaying the elements 
        foreach(string str in myList2) 
        
            Console.WriteLine(str); 
        
          
        Console.WriteLine("Trying to add new element into myList2:");
          
        // it will give error
        myList2.Add("HTML");
  
      
    

chevron_right


Output:

Before Wrapping: 
C
C++
Java
C#
Python
After Wrapping: 
C
C++
Java
C#
Python
Trying to add new element into myList2:

Runtime Error:

Unhandled Exception:
System.NotSupportedException: Collection is read-only.
at System.Collections.ArrayList+ReadOnlyArrayList.Add

Explanation: In the above program, you can add or remove the elements from the myList i.e original ArrayList which will reflect into the read-only collection.

Note:

  • To prevent any modifications to list, expose list only through this wrapper.
  • 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.
  • This method is an O(1) operation.

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.