C# | Creating a read-only wrapper for the List Last Updated : 27 Jan, 2019 Comments Improve Suggest changes Like Article Like Report List<T>.AsReadOnly Method is used to get a read-only ReadOnlyCollection<T> wrapper for the current collection. Syntax: public System.Collections.ObjectModel.ReadOnlyCollection AsReadOnly (); Return Value: It returns an object that acts as a read-only wrapper around the current List<T>. Example: CSharp // C# code to create a read-only // wrapper for the List<T> using System; using System.Collections; using System.Collections.Generic; class GFG { // Driver code public static void Main() { // Creating an List<T> of Integers List<int> firstlist = new List<int>(); // Adding elements to List firstlist.Add(1); firstlist.Add(2); firstlist.Add(3); firstlist.Add(4); firstlist.Add(5); firstlist.Add(6); firstlist.Add(7); Console.WriteLine("Before Wrapping: "); // Displaying the elements in the List foreach(int i in firstlist) { Console.WriteLine(i); } // Creating a Read-Only packing // around the List<T> IList<int> mylist2 = firstlist.AsReadOnly(); Console.WriteLine("After Wrapping: "); // Displaying the elements foreach(int m in mylist2) { Console.WriteLine(m); } Console.WriteLine("Trying to add new element into mylist2:"); // it will give error mylist2.Add(8); } } Output: 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: Runtime Error: Unhandled Exception: System.NotSupportedException: Collection is read-only. Note: 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: https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.asreadonly?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Creating a read-only wrapper for the List K Kirti_Mangal Follow Improve Article Tags : C# CSharp-method CSharp-Generic-List CSharp-Generic-Namespace Similar Reads C# | Creating a read-only wrapper for List List<T>.AsReadOnly Method is used to get a read-only ReadOnlyCollection<T> wrapper for the current collection. Syntax: public System.Collections.ObjectModel.ReadOnlyCollection<T> AsReadOnly (); Return Value: It returns an object that acts as a read-only wrapper around the current L 2 min read 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 2 min read C# | Check if the ArrayList is read-only ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dynamic memory allocation, adding, searching and sorting items in the list. ArrayList.IsReadOnly property is used to check whether the ArrayList is rea 2 min read C# | Check if a SortedList is read-only SortedList class is a collection of (key, value) pairs which are sorted according to keys. Those pairs can be accessible by key and as well as by index(zero-based indexing). This comes under System.Collections namespace. SortedList.IsReadOnly property is used to get a value which indicates that a So 2 min read C# | Get a read-only copy of the OrderedDictionary OrderedDictionary.AsReadOnly method returns a read-only copy of the current OrderedDictionary collection. Syntax: public System.Collections.Specialized.OrderedDictionary AsReadOnly (); Return Value: A read-only copy of the current OrderedDictionary collection. Below given are some examples to unders 2 min read Like