共用方式為


List<T>.AsReadOnly 方法

定義

傳回目前集合的唯讀 ReadOnlyCollection<T> 包裝函式。

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)

傳回

此物件作為唯讀包裝函式包住目前的 List<T>

範例

下列範例示範 AsReadOnly 方法。 List<T>系統會建立容量為 4 的字串,因為清單的最終大小已知為 4。 此清單會填入四個字串,並使用 AsReadOnly 方法來取得包裝原始清單的唯讀 IList<T> 泛型介面實作。

原始清單的元素會使用 Item[] C#) 索引器 (屬性設定為 “Coelophysis”,並再次顯示只讀清單的內容,以示範它只是原始清單的包裝函式。

using namespace System;
using namespace System::Collections::Generic;

void main()
{
    List<String^>^ dinosaurs = gcnew List<String^>(4);

    Console::WriteLine("\nCapacity: {0}", dinosaurs->Capacity);

    dinosaurs->Add("Tyrannosaurus");
    dinosaurs->Add("Amargasaurus");
    dinosaurs->Add("Mamenchisaurus");
    dinosaurs->Add("Deinonychus");

    Console::WriteLine();
    for each(String^ dinosaur in dinosaurs)
    {
        Console::WriteLine(dinosaur);
    }

    Console::WriteLine("\nIList<String^>^ roDinosaurs = dinosaurs->AsReadOnly()");
    IList<String^>^ roDinosaurs = dinosaurs->AsReadOnly();

    Console::WriteLine("\nElements in the read-only IList:");
    for each(String^ dinosaur in roDinosaurs)
    {
        Console::WriteLine(dinosaur);
    }

    Console::WriteLine("\ndinosaurs[2] = \"Coelophysis\"");
    dinosaurs[2] = "Coelophysis";

    Console::WriteLine("\nElements in the read-only IList:");
    for each(String^ dinosaur in roDinosaurs)
    {
        Console::WriteLine(dinosaur);
    }
}

/* This code example produces the following output:

Capacity: 4

Tyrannosaurus
Amargasaurus
Mamenchisaurus
Deinonychus

IList<String^>^ roDinosaurs = dinosaurs->AsReadOnly()

Elements in the read-only IList:
Tyrannosaurus
Amargasaurus
Mamenchisaurus
Deinonychus

dinosaurs[2] = "Coelophysis"

Elements in the read-only IList:
Tyrannosaurus
Amargasaurus
Coelophysis
Deinonychus
 */
using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        List<string> dinosaurs = new List<string>(4);

        Console.WriteLine("\nCapacity: {0}", dinosaurs.Capacity);

        dinosaurs.Add("Tyrannosaurus");
        dinosaurs.Add("Amargasaurus");
        dinosaurs.Add("Mamenchisaurus");
        dinosaurs.Add("Deinonychus");

        Console.WriteLine();
        foreach(string s in dinosaurs)
        {
            Console.WriteLine(s);
        }

        Console.WriteLine("\nIList<string> roDinosaurs = dinosaurs.AsReadOnly()");
        IList<string> roDinosaurs = dinosaurs.AsReadOnly();

        Console.WriteLine("\nElements in the read-only IList:");
        foreach(string dinosaur in roDinosaurs)
        {
            Console.WriteLine(dinosaur);
        }

        Console.WriteLine("\ndinosaurs[2] = \"Coelophysis\"");
        dinosaurs[2] = "Coelophysis";

        Console.WriteLine("\nElements in the read-only IList:");
        foreach(string dinosaur in roDinosaurs)
        {
            Console.WriteLine(dinosaur);
        }
    }
}

/* This code example produces the following output:

Capacity: 4

Tyrannosaurus
Amargasaurus
Mamenchisaurus
Deinonychus

IList<string> roDinosaurs = dinosaurs.AsReadOnly()

Elements in the read-only IList:
Tyrannosaurus
Amargasaurus
Mamenchisaurus
Deinonychus

dinosaurs[2] = "Coelophysis"

Elements in the read-only IList:
Tyrannosaurus
Amargasaurus
Coelophysis
Deinonychus
 */
Imports System.Collections.Generic

Public Class Example

    Public Shared Sub Main()

        Dim dinosaurs As New List(Of String)(4)

        Console.WriteLine(vbLf & "Capacity: {0}", dinosaurs.Capacity)

        dinosaurs.Add("Tyrannosaurus")
        dinosaurs.Add("Amargasaurus")
        dinosaurs.Add("Mamenchisaurus")
        dinosaurs.Add("Deinonychus")

        Console.WriteLine()
        For Each dinosaur As String In dinosaurs
            Console.WriteLine(dinosaur)
        Next

        Console.WriteLine(vbLf & _
            "Dim roDinosaurs As IList(Of String) = dinosaurs.AsReadOnly")
        Dim roDinosaurs As IList(Of String) = dinosaurs.AsReadOnly

        Console.WriteLine(vbLf & "Elements in the read-only IList:")
        For Each dinosaur As String In roDinosaurs
            Console.WriteLine(dinosaur)
        Next

        Console.WriteLine(vbLf & "dinosaurs(2) = ""Coelophysis""")
        dinosaurs(2) = "Coelophysis"

        Console.WriteLine(vbLf & "Elements in the read-only IList:")
        For Each dinosaur As String In roDinosaurs
            Console.WriteLine(dinosaur)
        Next

    End Sub
End Class

' This code example produces the following output:
'
'Capacity: 4
'
'Tyrannosaurus
'Amargasaurus
'Mamenchisaurus
'Deinonychus
'
'Dim roDinosaurs As IList(Of String) = dinosaurs.AsReadOnly
'
'Elements in the read-only IList:
'Tyrannosaurus
'Amargasaurus
'Mamenchisaurus
'Deinonychus
'
'dinosaurs(2) = "Coelophysis"
'
'Elements in the read-only IList:
'Tyrannosaurus
'Amargasaurus
'Coelophysis
'Deinonychus

備註

若要防止對 對象進行任何修改 List<T> ,請只透過這個包裝函式公開它。 ReadOnlyCollection<T>物件不會公開修改集合的方法。 不過,如果對基礎 List<T> 對象進行變更,只讀集合會反映這些變更。

這個方法是 O (1) 作業。

適用於