List<T>.AsReadOnly Method is used to get a read-only ReadOnlyCollection<T> wrapper for the current collection.
public System.Collections.ObjectModel.ReadOnlyCollection<T> AsReadOnly ();
Return Value: It returns an object that acts as a read-only wrapper around the current List<T>.
Before Wrapping: 17 19 21 9 75 19 73 After Wrapping: 17 19 21 9 75 19 73 Adding new element to mylist: 17 19 21 9 75 19 73 35 Trying to add new element into readlist:
System.NotSupportedException: Collection is read-only.
- C# | Creating a read-only wrapper for the List
- C# | Creating a synchronized (thread-safe) wrapper for a SortedList object
- C# | Creating a read-only wrapper for the ArrayList
- C# | Creating a synchronized (thread-safe) wrapper for the Hashtable
- C# | Creating a synchronized (thread-safe) wrapper for the ArrayList
- Difference between readonly and const keyword in C#
- Readonly in C#
- C# | Creating a HybridDictionary with specified initial size & case sensitivity
- 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 an ArrayList having specified initial capacity
- C# | Creating StringBuilder having specified capacity
- 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#
- How to sort a list in C# | List.Sort() Method Set -1
- How to sort a list in C# | List.Sort() Method Set -2
- C# | Adding an element to the List
- C# | Removing the specified element from the List
- C# | Removing all the elements from the List
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.