List<T>.AsReadOnly Méthode

Définition

Renvoie un wrapper en lecture seule ReadOnlyCollection<T> pour la collection active.

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)

Retours

Objet qui agit en tant que wrapper en lecture seule autour du wrapper actuel List<T>.

Exemples

L’exemple suivant illustre la AsReadOnly méthode. Une List<T> chaîne avec une capacité de 4 est créée, car la taille ultime de la liste est connue pour être exactement 4. La liste est remplie avec quatre chaînes et la AsReadOnly méthode est utilisée pour obtenir une implémentation d’interface générique en lecture seule IList<T> qui encapsule la liste d’origine.

Un élément de la liste d’origine est défini sur « Coelophysis » à l’aide de la Item[] propriété (l’indexeur en C#), et le contenu de la liste en lecture seule s’affiche à nouveau pour démontrer qu’il s’agit simplement d’un wrapper pour la liste d’origine.

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

Remarques

Pour empêcher toute modification de l’objet, exposez-le List<T> uniquement via ce wrapper. Un ReadOnlyCollection<T> objet n’expose pas les méthodes qui modifient la collection. Toutefois, si des modifications sont apportées à l’objet sous-jacent List<T> , la collection en lecture seule reflète ces modifications.

Cette méthode est une opération O(1).

S’applique à