ArrayList.ReadOnly(ArrayList) Method is used to get a read-only ArrayList wrapper.
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:
myList ArrayList is not read-only. myList2 ArrayList is read-only.
Before Wrapping: C C++ Java C# Python After Wrapping: C C++ Java C# Python Trying to add new element into myList2:
System.NotSupportedException: Collection is read-only.
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.
- 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.
- C# | Creating a synchronized (thread-safe) wrapper for the ArrayList
- C# | Creating a synchronized (thread-safe) wrapper for a SortedList object
- C# | Creating a synchronized (thread-safe) wrapper for the Hashtable
- C# | Creating a read-only wrapper for the List
- C# | Creating a read-only wrapper for List
- C# | Creating an ArrayList having specified initial capacity
- Difference between readonly and const keyword in C#
- Readonly in C#
- C# | Remove the first occurrence of a specific object from the ArrayList
- C# | Get the number of elements actually contained in the ArrayList
- C# | Check whether an element is contained in the ArrayList
- C# | Check if the ArrayList is read-only
- C# | Insert an element into the ArrayList at the specified index
- C# | Add an object to the end of the ArrayList
- C# | Copying the elements of ArrayList to a new array
- C# | Get or set the number of elements that the ArrayList can contain
- C# | Remove a range of elements from the ArrayList
- C# | Check if the ArrayList has a fixed size
- C# | Remove all elements from the ArrayList
- C# | Remove the element at the specified index of the ArrayList
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.