The AsReadOnly() method creates a read-only wrapper around an existing List<T>. It is useful when list data needs to be exposed for reading while keeping modification access restricted.
- Returns a ReadOnlyCollection<T>.
- Does not create a separate copy of the list.
- The wrapper reflects changes made to the original list.
using System;
using System.Collections.Generic;
class GFG{
static void Main(){
List<int> numbers = new List<int> { 10, 20, 30 };
var readOnlyNumbers = numbers.AsReadOnly();
foreach (int num in readOnlyNumbers)
Console.WriteLine(num);
}
}
Output
10 20 30
Explanation: AsReadOnly() creates a read-only wrapper around numbers. The elements can be accessed and iterated through using readOnlyNumbers, but the collection cannot be modified through this wrapper.
Syntax
ReadOnlyCollection<T> readOnlyList = list.AsReadOnly();
Here:
- list is the existing List<T> instance.
- AsReadOnly() creates a read-only wrapper around the list.
- readOnlyList stores the returned ReadOnlyCollection<T>.
If ReadOnlyCollection<T> is explicitly used as the variable type, include the following namespace:
using System.Collections.ObjectModel;
Return Value: The method returns a ReadOnlyCollection<T> that provides access to the elements of the source list without exposing collection-modification operations.
- Return type:
ReadOnlyCollection<T> - The returned collection references the original list rather than creating a copy.
- Changes made to the original list are visible through the returned collection.
How to Use AsReadOnly()
To use AsReadOnly(), create a List<T>, populate it with values, and call the method to obtain a read-only collection.
Steps to Use AsReadOnly()
- Create and populate a List<T>.
- Call AsReadOnly() on the list.
- Store the result in a ReadOnlyCollection<T> or implicitly typed variable.
- Read or iterate through the elements as required.
Example: The following example creates a read-only wrapper for a list of fruit names and displays its elements.
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
List<string> fruits = new List<string>
{
"Apple",
"Banana",
"Mango"
};
var readOnlyFruits = fruits.AsReadOnly();
foreach (string fruit in readOnlyFruits)
{
Console.WriteLine(fruit);
}
}
}
Output
Apple Banana Mango
Explanation: fruits.AsReadOnly() creates a ReadOnlyCollection<string> that exposes the three elements for reading. The foreach loop accesses these elements without modifying the collection.
AsReadOnly() vs Copying a List
AsReadOnly() provides a read-only view of the existing list, whereas copying creates a separate List<T> with its own collection structure.
| Feature | AsReadOnly() | Copying a List |
|---|---|---|
| Creates a separate list | No | Yes |
| Return type | ReadOnlyCollection<T> | List<T> |
| Modification through returned collection | Not allowed | Allowed |
| Reflects changes to original list | Yes | No |
| Independent collection | No | Yes |
| Main purpose | Provide read-only access | Create a separate list |
Example
using System.Collections.Generic;
List<int> numbers = new List<int> { 10, 20, 30 };
ReadOnlyCollection<int> readOnlyNumbers = numbers.AsReadOnly();
List<int> copiedNumbers = new List<int>(numbers);
Here, readOnlyNumbers is connected to the original numbers list, while copiedNumbers is a separate list.
Example:
numbers.Add(40);
After this change:
readOnlyNumbersalso contains40.copiedNumbersremains unchanged.
Advantages
AsReadOnly() is useful when you need to share list data while restricting collection-level modifications through a particular reference.
- Provides a read-only interface to an existing list.
- Avoids the overhead of creating a separate list copy.
- Keeps the data synchronized with the original list.
Limitations
AsReadOnly() restricts access through the wrapper but does not make the underlying list or its elements immutable.
- The original List<T> can still be modified.
- Changes to the source list are reflected in the read-only wrapper.
- Mutable objects stored in the list can still be changed through their own references.
- Use a separate copy when the collection needs to be independent of the original list.