List<T>.AsReadOnly Method is used to get a read-only ReadOnlyCollection<T> wrapper for the current collection.
public System.Collections.ObjectModel.ReadOnlyCollection AsReadOnly ();
Return Value: It returns an object that acts as a read-only wrapper around the current List<T>.
Before Wrapping: 1 2 3 4 5 6 7 After Wrapping: 1 2 3 4 5 6 7 Trying to add new element into mylist2:
System.NotSupportedException: Collection is read-only.
- 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 read-only wrapper for List
- C# | Creating a read-only wrapper for the ArrayList
- C# | Creating a synchronized (thread-safe) wrapper for the ArrayList
- C# | Creating a synchronized (thread-safe) wrapper for the Hashtable
- C# | Creating a synchronized (thread-safe) wrapper for a SortedList object
- Readonly in C#
- Difference between readonly and const keyword in C#
- C# | Creating StringBuilder having specified capacity
- C# | Creating an ArrayList having specified initial capacity
- C# | Creating an empty HybridDictionary with specified case sensitivity
- C# | Creating a Case-Sensitive HybridDictionary with specified initial size
- C# | Creating an empty case-sensitive HybridDictionary Class
- C# | Creating a HybridDictionary with specified initial size & case sensitivity
- Creating an Index From the Specified Index at the Start of a Collection in C#
- Creating an Index From the End of a Collection at a Specified Index Position in C#
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.