ReadOnlyCollection<T> 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
提供泛型只讀集合的基類。
generic <typename T>
public ref class ReadOnlyCollection : System::Collections::Generic::ICollection<T>, System::Collections::Generic::IEnumerable<T>, System::Collections::Generic::IList<T>, System::Collections::Generic::IReadOnlyCollection<T>, System::Collections::Generic::IReadOnlyList<T>, System::Collections::IList
generic <typename T>
public ref class ReadOnlyCollection : System::Collections::Generic::ICollection<T>, System::Collections::Generic::IEnumerable<T>, System::Collections::Generic::IList<T>, System::Collections::IList
public class ReadOnlyCollection<T> : System.Collections.Generic.ICollection<T>, System.Collections.Generic.IEnumerable<T>, System.Collections.Generic.IList<T>, System.Collections.Generic.IReadOnlyCollection<T>, System.Collections.Generic.IReadOnlyList<T>, System.Collections.IList
[System.Runtime.InteropServices.ComVisible(false)]
[System.Serializable]
public class ReadOnlyCollection<T> : System.Collections.Generic.ICollection<T>, System.Collections.Generic.IEnumerable<T>, System.Collections.Generic.IList<T>, System.Collections.IList
[System.Runtime.InteropServices.ComVisible(false)]
[System.Serializable]
public class ReadOnlyCollection<T> : System.Collections.Generic.ICollection<T>, System.Collections.Generic.IEnumerable<T>, System.Collections.Generic.IList<T>, System.Collections.Generic.IReadOnlyCollection<T>, System.Collections.Generic.IReadOnlyList<T>, System.Collections.IList
type ReadOnlyCollection<'T> = class
interface ICollection<'T>
interface seq<'T>
interface IEnumerable
interface IList<'T>
interface IReadOnlyCollection<'T>
interface IReadOnlyList<'T>
interface ICollection
interface IList
[<System.Runtime.InteropServices.ComVisible(false)>]
[<System.Serializable>]
type ReadOnlyCollection<'T> = class
interface IList<'T>
interface ICollection<'T>
interface seq<'T>
interface IList
interface ICollection
interface IEnumerable
[<System.Runtime.InteropServices.ComVisible(false)>]
[<System.Serializable>]
type ReadOnlyCollection<'T> = class
interface IList<'T>
interface ICollection<'T>
interface IList
interface ICollection
interface IReadOnlyList<'T>
interface IReadOnlyCollection<'T>
interface seq<'T>
interface IEnumerable
[<System.Runtime.InteropServices.ComVisible(false)>]
[<System.Serializable>]
type ReadOnlyCollection<'T> = class
interface IList<'T>
interface ICollection<'T>
interface seq<'T>
interface IEnumerable
interface IList
interface ICollection
interface IReadOnlyList<'T>
interface IReadOnlyCollection<'T>
type ReadOnlyCollection<'T> = class
interface IList<'T>
interface ICollection<'T>
interface IReadOnlyList<'T>
interface IReadOnlyCollection<'T>
interface seq<'T>
interface IList
interface ICollection
interface IEnumerable
Public Class ReadOnlyCollection(Of T)
Implements ICollection(Of T), IEnumerable(Of T), IList, IList(Of T), IReadOnlyCollection(Of T), IReadOnlyList(Of T)
Public Class ReadOnlyCollection(Of T)
Implements ICollection(Of T), IEnumerable(Of T), IList, IList(Of T)
類型參數
- T
集合中的項目類型。
- 繼承
-
ReadOnlyCollection<T>
- 衍生
- 屬性
- 實作
範例
下列程式代碼範例示範 ReadOnlyCollection<T> 類別的數個成員。 程式代碼範例會建立字串 List<T>,並在其中新增四個恐龍名稱。 程式代碼範例接著會將清單包裝在 ReadOnlyCollection<T>中。
在示範 Count、Contains、Item[]和 IList.IndexOf 成員之後,程式代碼範例會示範 ReadOnlyCollection<T> 只是原始 List<T> 的包裝函式,方法是將新專案新增至 List<T>,並顯示 ReadOnlyCollection<T>的內容。
最後,程式代碼範例會建立大於集合的陣列,並使用 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"
'""
備註
ReadOnlyCollection<T> 泛型類別的實例一律為唯讀。 只讀的集合只是具有防止修改集合之包裝函式的集合;因此,如果對基礎集合進行變更,只讀集合會反映這些變更。 如需這個類別的可修改版本,請參閱 Collection<T>。
給繼承者的注意事項
此基類可供實作者更輕鬆地建立泛型只讀自定義集合。 鼓勵實作者擴充此基類,而不是建立自己的基類。
建構函式
ReadOnlyCollection<T>(IList<T>) |
初始化 ReadOnlyCollection<T> 類別的新實例,這個實例是指定清單周圍的唯讀包裝函式。 |
屬性
Count |
取得包含在 ReadOnlyCollection<T> 實例中的項目數目。 |
Empty | |
Item[Int32] |
取得位於指定索引處的專案。 |
Items |
傳回 ReadOnlyCollection<T> 包裝的 IList<T>。 |
方法
Contains(T) |
判斷專案是否在 ReadOnlyCollection<T>中。 |
CopyTo(T[], Int32) |
從目標陣列的指定索引開始,將整個 ReadOnlyCollection<T> 複製到相容的一維 Array。 |
Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
GetEnumerator() |
傳回逐一查看 ReadOnlyCollection<T>的列舉值。 |
GetHashCode() |
做為預設哈希函式。 (繼承來源 Object) |
GetType() |
取得目前實例的 Type。 (繼承來源 Object) |
IndexOf(T) |
搜尋指定的物件,並傳回整個 ReadOnlyCollection<T>中第一個出現專案的以零起始的索引。 |
MemberwiseClone() |
建立目前 Object的淺層複本。 (繼承來源 Object) |
ToString() |
傳回表示目前 物件的字串。 (繼承來源 Object) |
明確介面實作
擴充方法
適用於
執行緒安全性
此類型的公用靜態 (Shared
) 成員是安全線程。 不保證任何實例成員都是安全線程。
只要集合未修改,ReadOnlyCollection<T> 就可以同時支援多個讀取器。 即便如此,透過集合列舉本質上不是安全線程的程式。 若要保證列舉期間的線程安全性,您可以在整個列舉期間鎖定集合。 若要允許多個線程存取集合以進行讀取和寫入,您必須實作自己的同步處理。