List<T>.AsReadOnly Metodo

Definizione

Restituisce un wrapper di sola lettura ReadOnlyCollection<T> per l'insieme corrente.

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)

Valori restituiti

Oggetto che funge da wrapper di sola lettura intorno all'oggetto corrente List<T>.

Esempio

Nell'esempio seguente viene illustrato il AsReadOnly metodo . Viene List<T> creato un di stringhe con capacità pari a 4, perché la dimensione finale dell'elenco è nota esattamente come 4. L'elenco viene popolato con quattro stringhe e il AsReadOnly metodo viene usato per ottenere un'implementazione di interfaccia generica di sola IList<T> lettura che esegue il wrapping dell'elenco originale.

Un elemento dell'elenco originale è impostato su "Coelophysis" usando la Item[] proprietà (l'indicizzatore in C#) e il contenuto dell'elenco di sola lettura viene visualizzato di nuovo per dimostrare che è solo un wrapper per l'elenco originale.

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

Commenti

Per evitare modifiche all'oggetto List<T> , esporlo solo tramite questo wrapper. Un ReadOnlyCollection<T> oggetto non espone metodi che modificano la raccolta. Tuttavia, se vengono apportate modifiche all'oggetto sottostante List<T> , l'insieme di sola lettura riflette tali modifiche.

Questo metodo è un'operazione O(1).

Si applica a