List<T>.AsReadOnly Methode

Definition

Gibt einen schreibgeschützten ReadOnlyCollection<T> Wrapper für die aktuelle Auflistung zurück.

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)

Gibt zurück

Ein Objekt, das als schreibgeschützter Wrapper um das aktuelle List<T>Objekt fungiert.

Beispiele

Im folgenden Beispiel wird die AsReadOnly Methode veranschaulicht. Eine List<T> Von Zeichenfolgen mit einer Kapazität von 4 wird erstellt, da die ultimative Größe der Liste bekannt ist, genau 4. Die Liste wird mit vier Zeichenfolgen aufgefüllt, und die AsReadOnly Methode wird verwendet, um eine schreibgeschützte IList<T> generische Schnittstellenimplementierung abzurufen, die die ursprüngliche Liste umschließt.

Ein Element der ursprünglichen Liste wird mithilfe der Item[] Eigenschaft (der Indexer in C#) auf "Coelophysis" festgelegt, und der Inhalt der schreibgeschützten Liste wird erneut angezeigt, um zu veranschaulichen, dass es sich nur um einen Wrapper für die ursprüngliche Liste handelt.

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

Hinweise

Um Änderungen am List<T> Objekt zu verhindern, machen Sie es nur über diesen Wrapper verfügbar. Ein ReadOnlyCollection<T> Objekt macht keine Methoden verfügbar, die die Auflistung ändern. Wenn jedoch Änderungen am zugrunde liegenden List<T> Objekt vorgenommen werden, spiegelt die schreibgeschützte Auflistung diese Änderungen wider.

Diese Methode ist ein O(1)-Vorgang.

Gilt für: