List<T>.AsReadOnly Método
Definición
Importante
Parte de la información hace referencia a la versión preliminar del producto, que puede haberse modificado sustancialmente antes de lanzar la versión definitiva. Microsoft no otorga ninguna garantía, explícita o implícita, con respecto a la información proporcionada aquí.
Devuelve un contenedor de solo ReadOnlyCollection<T> lectura para la colección actual.
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)
Devoluciones
Objeto que actúa como un contenedor de solo lectura alrededor del objeto actual List<T>.
Ejemplos
En el ejemplo siguiente se muestra el AsReadOnly método . Se List<T> crea una de cadenas con una capacidad de 4, ya que se sabe que el tamaño final de la lista es exactamente 4. La lista se rellena con cuatro cadenas y el AsReadOnly método se usa para obtener una implementación de interfaz genérica de solo IList<T> lectura que encapsula la lista original.
Un elemento de la lista original se establece en "Coelophysis" mediante la Item[] propiedad (el indexador en C#) y el contenido de la lista de solo lectura se muestra de nuevo para demostrar que es solo un contenedor para la lista original.
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
Comentarios
Para evitar modificaciones en el List<T> objeto, exponga solo a través de este contenedor. Un ReadOnlyCollection<T> objeto no expone métodos que modifican la colección. Sin embargo, si se realizan cambios en el objeto subyacente List<T> , la colección de solo lectura refleja esos cambios.
Este método es una operación O(1).