ReadOnlyCollection<T>.Items 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
傳回 IList<T> 所包裝的 ReadOnlyCollection<T>。
protected:
property System::Collections::Generic::IList<T> ^ Items { System::Collections::Generic::IList<T> ^ get(); };
protected System.Collections.Generic.IList<T> Items { get; }
member this.Items : System.Collections.Generic.IList<'T>
Protected ReadOnly Property Items As IList(Of T)
屬性值
IList<T> 所包裝的 ReadOnlyCollection<T>。
範例
下列程式代碼範例示範 類別的數個 ReadOnlyCollection<T> 成員。 程式代碼範例會建立字串的 ,並在其中新增四個 List<T> 恐龍名稱。 然後,程式代碼範例會將清單包裝在 中 ReadOnlyCollection<T>。
在示範 、、 和 成員之後,程式代碼範例會示範 ReadOnlyCollection<T> 只是原始List<T>的包裝函式,方法是將新專案新增至 List<T> ,並顯示 的內容ReadOnlyCollection<T>。IList.IndexOfItem[]ContainsCount
最後,程式代碼範例會建立大於集合的陣列,並使用 CopyTo 方法將集合的專案插入數位中間。
using namespace System;
using namespace System::Collections::Generic;
using namespace System::Collections::ObjectModel;
void main()
{
List<String^>^ dinosaurs = gcnew List<String^>();
dinosaurs->Add("Tyrannosaurus");
dinosaurs->Add("Amargasaurus");
dinosaurs->Add("Deinonychus");
dinosaurs->Add("Compsognathus");
ReadOnlyCollection<String^>^ readOnlyDinosaurs =
gcnew ReadOnlyCollection<String^>(dinosaurs);
Console::WriteLine();
for each(String^ dinosaur in readOnlyDinosaurs )
{
Console::WriteLine(dinosaur);
}
Console::WriteLine("\nCount: {0}", readOnlyDinosaurs->Count);
Console::WriteLine("\nContains(\"Deinonychus\"): {0}",
readOnlyDinosaurs->Contains("Deinonychus"));
Console::WriteLine("\nreadOnlyDinosaurs[3]: {0}",
readOnlyDinosaurs[3]);
Console::WriteLine("\nIndexOf(\"Compsognathus\"): {0}",
readOnlyDinosaurs->IndexOf("Compsognathus"));
Console::WriteLine("\nInsert into the wrapped List:");
Console::WriteLine("Insert(2, \"Oviraptor\")");
dinosaurs->Insert(2, "Oviraptor");
Console::WriteLine();
for each( String^ dinosaur in readOnlyDinosaurs )
{
Console::WriteLine(dinosaur);
}
array<String^>^ dinoArray =
gcnew array<String^>(readOnlyDinosaurs->Count + 2);
readOnlyDinosaurs->CopyTo(dinoArray, 1);
Console::WriteLine("\nCopied array has {0} elements:",
dinoArray->Length);
for each( String^ dinosaur in dinoArray )
{
Console::WriteLine("\"{0}\"", dinosaur);
}
}
/* This code example produces the following output:
Tyrannosaurus
Amargasaurus
Deinonychus
Compsognathus
Count: 4
Contains("Deinonychus"): True
readOnlyDinosaurs[3]: Compsognathus
IndexOf("Compsognathus"): 3
Insert into the wrapped List:
Insert(2, "Oviraptor")
Tyrannosaurus
Amargasaurus
Oviraptor
Deinonychus
Compsognathus
Copied array has 7 elements:
""
"Tyrannosaurus"
"Amargasaurus"
"Oviraptor"
"Deinonychus"
"Compsognathus"
""
*/
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
public class Example
{
public static void Main()
{
List<string> dinosaurs = new List<string>();
dinosaurs.Add("Tyrannosaurus");
dinosaurs.Add("Amargasaurus");
dinosaurs.Add("Deinonychus");
dinosaurs.Add("Compsognathus");
ReadOnlyCollection<string> readOnlyDinosaurs =
new ReadOnlyCollection<string>(dinosaurs);
Console.WriteLine();
foreach( string dinosaur in readOnlyDinosaurs )
{
Console.WriteLine(dinosaur);
}
Console.WriteLine("\nCount: {0}", readOnlyDinosaurs.Count);
Console.WriteLine("\nContains(\"Deinonychus\"): {0}",
readOnlyDinosaurs.Contains("Deinonychus"));
Console.WriteLine("\nreadOnlyDinosaurs[3]: {0}",
readOnlyDinosaurs[3]);
Console.WriteLine("\nIndexOf(\"Compsognathus\"): {0}",
readOnlyDinosaurs.IndexOf("Compsognathus"));
Console.WriteLine("\nInsert into the wrapped List:");
Console.WriteLine("Insert(2, \"Oviraptor\")");
dinosaurs.Insert(2, "Oviraptor");
Console.WriteLine();
foreach( string dinosaur in readOnlyDinosaurs )
{
Console.WriteLine(dinosaur);
}
string[] dinoArray = new string[readOnlyDinosaurs.Count + 2];
readOnlyDinosaurs.CopyTo(dinoArray, 1);
Console.WriteLine("\nCopied array has {0} elements:",
dinoArray.Length);
foreach( string dinosaur in dinoArray )
{
Console.WriteLine("\"{0}\"", dinosaur);
}
}
}
/* This code example produces the following output:
Tyrannosaurus
Amargasaurus
Deinonychus
Compsognathus
Count: 4
Contains("Deinonychus"): True
readOnlyDinosaurs[3]: Compsognathus
IndexOf("Compsognathus"): 3
Insert into the wrapped List:
Insert(2, "Oviraptor")
Tyrannosaurus
Amargasaurus
Oviraptor
Deinonychus
Compsognathus
Copied array has 7 elements:
""
"Tyrannosaurus"
"Amargasaurus"
"Oviraptor"
"Deinonychus"
"Compsognathus"
""
*/
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Public Class Example
Public Shared Sub Main()
Dim dinosaurs As New List(Of String)
dinosaurs.Add("Tyrannosaurus")
dinosaurs.Add("Amargasaurus")
dinosaurs.Add("Deinonychus")
dinosaurs.Add("Compsognathus")
Dim readOnlyDinosaurs As _
New ReadOnlyCollection(Of String)(dinosaurs)
Console.WriteLine()
For Each dinosaur As String In readOnlyDinosaurs
Console.WriteLine(dinosaur)
Next
Console.WriteLine(vbLf & "Count: {0}", _
readOnlyDinosaurs.Count)
Console.WriteLine(vbLf & "Contains(""Deinonychus""): {0}", _
readOnlyDinosaurs.Contains("Deinonychus"))
Console.WriteLine(vbLf & _
"readOnlyDinosaurs(3): {0}", readOnlyDinosaurs(3))
Console.WriteLine(vbLf & "IndexOf(""Compsognathus""): {0}", _
readOnlyDinosaurs.IndexOf("Compsognathus"))
Console.WriteLine(vbLf & "Insert into the wrapped List:")
Console.WriteLine("Insert(2, ""Oviraptor"")")
dinosaurs.Insert(2, "Oviraptor")
Console.WriteLine()
For Each dinosaur As String In readOnlyDinosaurs
Console.WriteLine(dinosaur)
Next
Dim dinoArray(readOnlyDinosaurs.Count + 1) As String
readOnlyDinosaurs.CopyTo(dinoArray, 1)
Console.WriteLine(vbLf & "Copied array has {0} elements:", _
dinoArray.Length)
For Each dinosaur As String In dinoArray
Console.WriteLine("""{0}""", dinosaur)
Next
End Sub
End Class
' This code example produces the following output:
'
'Tyrannosaurus
'Amargasaurus
'Deinonychus
'Compsognathus
'
'Count: 4
'
'Contains("Deinonychus"): True
'
'readOnlyDinosaurs(3): Compsognathus
'
'IndexOf("Compsognathus"): 3
'
'Insert into the wrapped List:
'Insert(2, "Oviraptor")
'
'Tyrannosaurus
'Amargasaurus
'Oviraptor
'Deinonychus
'Compsognathus
'
'Copied array has 7 elements:
'""
'"Tyrannosaurus"
'"Amargasaurus"
'"Oviraptor"
'"Deinonychus"
'"Compsognathus"
'""
備註
擷取這個屬性的值是一種 O(1) 運算。
給繼承者的注意事項
提供這個屬性以允許直接存取隱藏之 IList<T>ReadOnlyCollection<T> 的成員。