List<T>.AsReadOnly Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Returns a read-only ReadOnlyCollection<T> wrapper for the current collection.
public:
System::Collections::ObjectModel::ReadOnlyCollection<T> ^ AsReadOnly();
public System.Collections.ObjectModel.ReadOnlyCollection<T> AsReadOnly();
member this.AsReadOnly : unit -> System.Collections.ObjectModel.ReadOnlyCollection<'T>
Public Function AsReadOnly () As ReadOnlyCollection(Of T)
Returns
An object that acts as a read-only wrapper around the current List<T>.
Examples
The following example demonstrates the AsReadOnly method. A List<T> of strings with a capacity of 4 is created, because the ultimate size of the list is known to be exactly 4. The list is populated with four strings, and the AsReadOnly method is used to get a read-only IList<T> generic interface implementation that wraps the original list.
An element of the original list is set to "Coelophysis" using the Item[] property (the indexer in C#), and the contents of the read-only list are displayed again to demonstrate that it is just a wrapper for the original list.
using System;
using System.Collections.Generic;
public partial class Program
{
public static void Main()
{
List<string> animals = new List<string>(4);
Console.WriteLine("\nCapacity: {0}", animals.Capacity);
animals.Add("Cat");
animals.Add("Dog");
animals.Add("Squirrel");
animals.Add("Wolf");
Console.WriteLine();
foreach (string animal in animals)
{
Console.WriteLine(animal);
}
Console.WriteLine("\nIList<string> roAnimals = animals.AsReadOnly()");
IList<string> roAnimals = animals.AsReadOnly();
Console.WriteLine("\nElements in the read-only IList:");
foreach (string animal in roAnimals)
{
Console.WriteLine(animal);
}
Console.WriteLine("\nanimals[2] = \"Lion\"");
animals[2] = "Lion";
Console.WriteLine("\nElements in the read-only IList:");
foreach (string animal in roAnimals)
{
Console.WriteLine(animal);
}
}
}
/*
This code example produces the following output:
Capacity: 4
Cat
Dog
Squirrel
Wolf
IList<string> roAnimals = animals.AsReadOnly()
Elements in the read-only IList:
Cat
Dog
Squirrel
Wolf
animals[2] = "Lion"
Elements in the read-only IList:
Cat
Dog
Lion
Wolf
*/
Imports System.Collections.Generic
Partial Public Class Program
Public Shared Sub Main()
Dim animals As New List(Of String)(4)
Console.WriteLine(vbLf & "Capacity: {0}", animals.Capacity)
animals.Add("Cat")
animals.Add("Dog")
animals.Add("Squirrel")
animals.Add("Wolf")
Console.WriteLine()
For Each animal As String In animals
Console.WriteLine(animal)
Next
Console.WriteLine(vbLf & _
"Dim roAnimals As IList(Of String) = animals.AsReadOnly")
Dim roAnimals As IList(Of String) = animals.AsReadOnly
Console.WriteLine(vbLf & "Elements in the read-only IList:")
For Each animal As String In roAnimals
Console.WriteLine(animal)
Next
Console.WriteLine(vbLf & "animals(2) = ""Lion""")
animals(2) = "Lion"
Console.WriteLine(vbLf & "Elements in the read-only IList:")
For Each animal As String In roAnimals
Console.WriteLine(animal)
Next
End Sub
End Class
' This code example produces the following output:
'
' Capacity: 4
'
' Cat
' Dog
' Squirrel
' Wolf
'
' Dim roAnimals As IList(Of String) = animals.AsReadOnly
'
' Elements in the read-only IList:
' Cat
' Dog
' Squirrel
' Wolf
'
' animals(2) = "Lion"
'
' Elements in the read-only IList:
' Cat
' Dog
' Lion
' Wolf
Remarks
To prevent any modifications to the List<T> object, expose it only through this wrapper. A ReadOnlyCollection<T> object does not expose methods that modify the collection. However, if changes are made to the underlying List<T> object, the read-only collection reflects those changes.
This method is an O(1) operation.