List<T>.AsReadOnly メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
現在のコレクションの読み取り専用 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 メソッドを示します。 リストの最終的なサイズは正確に 4 であることがわかっているため、容量が 4 の文字列の List<T> が作成されます。 リストには 4 つの文字列が設定され、 AsReadOnly メソッドは、元のリストをラップする読み取り専用 IList<T> ジェネリック インターフェイス実装を取得するために使用されます。
元のリストの要素は、 Item[] プロパティ (C# のインデクサー) を使用して "Coelophysis" に設定され、読み取り専用リストの内容が再び表示され、元のリストのラッパーであることを示します。
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
注釈
List<T> オブジェクトに変更を加えないようにするには、このラッパーを介してのみ公開します。 ReadOnlyCollection<T> オブジェクトは、コレクションを変更するメソッドを公開しません。 ただし、基になる List<T> オブジェクトに変更が加えられた場合、読み取り専用コレクションにはそれらの変更が反映されます。
このメソッドは O(1) 操作です。