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 . List<T> 목록의 최종 크기는 정확히 4로 알려져 있으므로 용량이 4인 문자열이 만들어집니다. 목록은 4개의 문자열로 채워지고 AsReadOnly 이 메서드는 원래 목록을 래핑하는 읽기 전용 IList<T> 제네릭 인터페이스 구현을 가져오는 데 사용됩니다.
원래 목록의 요소는 속성(C#의 인덱서)을 사용하여 Item[] "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) 작업입니다.