C# | Creating a read-only wrapper for List Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share 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<T> 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.Generic; class GFG { // Driver code public static void Main() { // Creating an List<T> of Integers List<int> mylist = new List<int>(); // Adding elements to List mylist.Add(17); mylist.Add(19); mylist.Add(21); mylist.Add(9); mylist.Add(75); mylist.Add(19); mylist.Add(73); Console.WriteLine("Before Wrapping: "); // Displaying the elements // in the mylist foreach(int i in mylist) { Console.WriteLine(i); } // Creating a Read-Only packing // around the List IList<int> readlist = mylist.AsReadOnly(); Console.WriteLine("After Wrapping: "); // Displaying the elements // of Read-Only Collection foreach(int j in readlist) { Console.WriteLine(j); } // You can add elements to // the original List i.e. mylist Console.WriteLine("Adding new element to mylist: "); mylist.Add(35); // Displaying the elements // in the mylist foreach(int k in mylist) { Console.WriteLine(k); } // But you cannot add elements // into the Read-Only Collection Console.WriteLine("Trying to add new element into readlist:"); // it will give error readlist.Add(34); } } Output: 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: Runtime Error: Unhandled Exception: System.NotSupportedException: Collection is read-only. Reference: https://learn.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 the 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 AsReadOnly (); Return Value: It returns an object that acts as a read-only wrapper around the current List<T 2 min read C# | Creating a read-only wrapper for the 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 AsReadOnly (); Return Value: It returns an object that acts as a read-only wrapper around the current List<T 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# | 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 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# | 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 Like