Collection<T> 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
提供泛型集合的基底類別。
generic <typename T>
public ref class Collection : 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 Collection : System::Collections::Generic::ICollection<T>, System::Collections::Generic::IEnumerable<T>, System::Collections::Generic::IList<T>, System::Collections::IList
public class Collection<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 Collection<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 Collection<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 Collection<'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 Collection<'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 Collection<'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 Collection<'T> = class
interface IList<'T>
interface ICollection<'T>
interface seq<'T>
interface IEnumerable
interface IList
interface ICollection
interface IReadOnlyList<'T>
interface IReadOnlyCollection<'T>
type Collection<'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 Collection(Of T)
Implements ICollection(Of T), IEnumerable(Of T), IList, IList(Of T), IReadOnlyCollection(Of T), IReadOnlyList(Of T)
Public Class Collection(Of T)
Implements ICollection(Of T), IEnumerable(Of T), IList, IList(Of T)
類型參數
- T
集合中項目的類型。
- 繼承
-
Collection<T>
- 衍生
- 屬性
- 實作
範例
本節包含兩個程式碼範例。 第一個範例示範 類別的數個 Collection<T> 屬性和方法。 第二個範例示範如何從 建構的 Collection<T>型別衍生集合類別,以及如何覆寫 的 Collection<T> 受保護方法來提供自定義行為。
範例 1
下列程式代碼範例示範 的許多 屬性和方法 Collection<T>。 此程式代碼範例會建立字串的集合、使用 Add 方法來新增數個字元串、顯示 Count,並列出字元串。 此範例會 IndexOf 使用 方法來尋找字串的索引和 Contains 方法來判斷字串是否在集合中。 此範例會使用 方法插入字串, Insert 並使用預設 Item[] 屬性擷取和設定字串, (C#) 中的索引器。 此範例會使用 方法,以及使用 RemoveAt 方法的索引,依Remove字串識別移除字串。 最後, Clear 方法用來清除集合中的所有字串。
using namespace System;
using namespace System::Collections::Generic;
using namespace System::Collections::ObjectModel;
public ref class Demo
{
public:
static void Main()
{
Collection<String^>^ dinosaurs = gcnew Collection<String^>();
dinosaurs->Add("Psitticosaurus");
dinosaurs->Add("Caudipteryx");
dinosaurs->Add("Compsognathus");
dinosaurs->Add("Muttaburrasaurus");
Console::WriteLine("{0} dinosaurs:", dinosaurs->Count);
Display(dinosaurs);
Console::WriteLine("\nIndexOf(\"Muttaburrasaurus\"): {0}",
dinosaurs->IndexOf("Muttaburrasaurus"));
Console::WriteLine("\nContains(\"Caudipteryx\"): {0}",
dinosaurs->Contains("Caudipteryx"));
Console::WriteLine("\nInsert(2, \"Nanotyrannus\")");
dinosaurs->Insert(2, "Nanotyrannus");
Display(dinosaurs);
Console::WriteLine("\ndinosaurs[2]: {0}", dinosaurs[2]);
Console::WriteLine("\ndinosaurs[2] = \"Microraptor\"");
dinosaurs[2] = "Microraptor";
Display(dinosaurs);
Console::WriteLine("\nRemove(\"Microraptor\")");
dinosaurs->Remove("Microraptor");
Display(dinosaurs);
Console::WriteLine("\nRemoveAt(0)");
dinosaurs->RemoveAt(0);
Display(dinosaurs);
Console::WriteLine("\ndinosaurs.Clear()");
dinosaurs->Clear();
Console::WriteLine("Count: {0}", dinosaurs->Count);
}
private:
static void Display(Collection<String^>^ cs)
{
Console::WriteLine();
for each( String^ item in cs )
{
Console::WriteLine(item);
}
}
};
int main()
{
Demo::Main();
}
/* This code example produces the following output:
4 dinosaurs:
Psitticosaurus
Caudipteryx
Compsognathus
Muttaburrasaurus
IndexOf("Muttaburrasaurus"): 3
Contains("Caudipteryx"): True
Insert(2, "Nanotyrannus")
Psitticosaurus
Caudipteryx
Nanotyrannus
Compsognathus
Muttaburrasaurus
dinosaurs[2]: Nanotyrannus
dinosaurs[2] = "Microraptor"
Psitticosaurus
Caudipteryx
Microraptor
Compsognathus
Muttaburrasaurus
Remove("Microraptor")
Psitticosaurus
Caudipteryx
Compsognathus
Muttaburrasaurus
RemoveAt(0)
Caudipteryx
Compsognathus
Muttaburrasaurus
dinosaurs.Clear()
Count: 0
*/
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
public class Demo
{
public static void Main()
{
Collection<string> dinosaurs = new Collection<string>();
dinosaurs.Add("Psitticosaurus");
dinosaurs.Add("Caudipteryx");
dinosaurs.Add("Compsognathus");
dinosaurs.Add("Muttaburrasaurus");
Console.WriteLine("{0} dinosaurs:", dinosaurs.Count);
Display(dinosaurs);
Console.WriteLine("\nIndexOf(\"Muttaburrasaurus\"): {0}",
dinosaurs.IndexOf("Muttaburrasaurus"));
Console.WriteLine("\nContains(\"Caudipteryx\"): {0}",
dinosaurs.Contains("Caudipteryx"));
Console.WriteLine("\nInsert(2, \"Nanotyrannus\")");
dinosaurs.Insert(2, "Nanotyrannus");
Display(dinosaurs);
Console.WriteLine("\ndinosaurs[2]: {0}", dinosaurs[2]);
Console.WriteLine("\ndinosaurs[2] = \"Microraptor\"");
dinosaurs[2] = "Microraptor";
Display(dinosaurs);
Console.WriteLine("\nRemove(\"Microraptor\")");
dinosaurs.Remove("Microraptor");
Display(dinosaurs);
Console.WriteLine("\nRemoveAt(0)");
dinosaurs.RemoveAt(0);
Display(dinosaurs);
Console.WriteLine("\ndinosaurs.Clear()");
dinosaurs.Clear();
Console.WriteLine("Count: {0}", dinosaurs.Count);
}
private static void Display(Collection<string> cs)
{
Console.WriteLine();
foreach( string item in cs )
{
Console.WriteLine(item);
}
}
}
/* This code example produces the following output:
4 dinosaurs:
Psitticosaurus
Caudipteryx
Compsognathus
Muttaburrasaurus
IndexOf("Muttaburrasaurus"): 3
Contains("Caudipteryx"): True
Insert(2, "Nanotyrannus")
Psitticosaurus
Caudipteryx
Nanotyrannus
Compsognathus
Muttaburrasaurus
dinosaurs[2]: Nanotyrannus
dinosaurs[2] = "Microraptor"
Psitticosaurus
Caudipteryx
Microraptor
Compsognathus
Muttaburrasaurus
Remove("Microraptor")
Psitticosaurus
Caudipteryx
Compsognathus
Muttaburrasaurus
RemoveAt(0)
Caudipteryx
Compsognathus
Muttaburrasaurus
dinosaurs.Clear()
Count: 0
*/
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Public Class Demo
Public Shared Sub Main()
Dim dinosaurs As New Collection(Of String)
dinosaurs.Add("Psitticosaurus")
dinosaurs.Add("Caudipteryx")
dinosaurs.Add("Compsognathus")
dinosaurs.Add("Muttaburrasaurus")
Console.WriteLine("{0} dinosaurs:", dinosaurs.Count)
Display(dinosaurs)
Console.WriteLine(vbLf & "IndexOf(""Muttaburrasaurus""): {0}", _
dinosaurs.IndexOf("Muttaburrasaurus"))
Console.WriteLine(vbLf & "Contains(""Caudipteryx""): {0}", _
dinosaurs.Contains("Caudipteryx"))
Console.WriteLine(vbLf & "Insert(2, ""Nanotyrannus"")")
dinosaurs.Insert(2, "Nanotyrannus")
Display(dinosaurs)
Console.WriteLine(vbLf & "dinosaurs(2): {0}", dinosaurs(2))
Console.WriteLine(vbLf & "dinosaurs(2) = ""Microraptor""")
dinosaurs(2) = "Microraptor"
Display(dinosaurs)
Console.WriteLine(vbLf & "Remove(""Microraptor"")")
dinosaurs.Remove("Microraptor")
Display(dinosaurs)
Console.WriteLine(vbLf & "RemoveAt(0)")
dinosaurs.RemoveAt(0)
Display(dinosaurs)
Console.WriteLine(vbLf & "dinosaurs.Clear()")
dinosaurs.Clear()
Console.WriteLine("Count: {0}", dinosaurs.Count)
End Sub
Private Shared Sub Display(ByVal cs As Collection(Of String))
Console.WriteLine()
For Each item As String In cs
Console.WriteLine(item)
Next item
End Sub
End Class
' This code example produces the following output:
'
'4 dinosaurs:
'
'Psitticosaurus
'Caudipteryx
'Compsognathus
'Muttaburrasaurus
'
'IndexOf("Muttaburrasaurus"): 3
'
'Contains("Caudipteryx"): True
'
'Insert(2, "Nanotyrannus")
'
'Psitticosaurus
'Caudipteryx
'Nanotyrannus
'Compsognathus
'Muttaburrasaurus
'
'dinosaurs(2): Nanotyrannus
'
'dinosaurs(2) = "Microraptor"
'
'Psitticosaurus
'Caudipteryx
'Microraptor
'Compsognathus
'Muttaburrasaurus
'
'Remove("Microraptor")
'
'Psitticosaurus
'Caudipteryx
'Compsognathus
'Muttaburrasaurus
'
'RemoveAt(0)
'
'Caudipteryx
'Compsognathus
'Muttaburrasaurus
'
'dinosaurs.Clear()
'Count: 0
範例 2
下列程式代碼範例示範如何從泛型類別的Collection<T>建構型別衍生集合類別,以及如何覆寫受保護的InsertItem、RemoveItem、 ClearItems和 SetItem 方法,以提供、Insert、 Remove和 Clear 方法的自定義行為Add,以及設定 Item[] 屬性。
此範例提供的自定義行為是在 Changed
每個受保護方法結尾引發的通知事件。 類別 Dinosaurs
會 Collection<string>
繼承 Visual Basic) 中的 (Collection(Of String)
,並定義 Changed
事件,該事件會使用 DinosaursChangedEventArgs
類別來取得事件資訊,以及用來識別變更類型的列舉。
程式代碼範例會呼叫 的 Collection<T> 數個屬性和方法來示範自定義事件。
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
public class Dinosaurs : Collection<string>
{
public event EventHandler<DinosaursChangedEventArgs> Changed;
protected override void InsertItem(int index, string newItem)
{
base.InsertItem(index, newItem);
EventHandler<DinosaursChangedEventArgs> temp = Changed;
if (temp != null)
{
temp(this, new DinosaursChangedEventArgs(
ChangeType.Added, newItem, null));
}
}
protected override void SetItem(int index, string newItem)
{
string replaced = Items[index];
base.SetItem(index, newItem);
EventHandler<DinosaursChangedEventArgs> temp = Changed;
if (temp != null)
{
temp(this, new DinosaursChangedEventArgs(
ChangeType.Replaced, replaced, newItem));
}
}
protected override void RemoveItem(int index)
{
string removedItem = Items[index];
base.RemoveItem(index);
EventHandler<DinosaursChangedEventArgs> temp = Changed;
if (temp != null)
{
temp(this, new DinosaursChangedEventArgs(
ChangeType.Removed, removedItem, null));
}
}
protected override void ClearItems()
{
base.ClearItems();
EventHandler<DinosaursChangedEventArgs> temp = Changed;
if (temp != null)
{
temp(this, new DinosaursChangedEventArgs(
ChangeType.Cleared, null, null));
}
}
}
// Event argument for the Changed event.
//
public class DinosaursChangedEventArgs : EventArgs
{
public readonly string ChangedItem;
public readonly ChangeType ChangeType;
public readonly string ReplacedWith;
public DinosaursChangedEventArgs(ChangeType change, string item,
string replacement)
{
ChangeType = change;
ChangedItem = item;
ReplacedWith = replacement;
}
}
public enum ChangeType
{
Added,
Removed,
Replaced,
Cleared
};
public class Demo
{
public static void Main()
{
Dinosaurs dinosaurs = new Dinosaurs();
dinosaurs.Changed += ChangedHandler;
dinosaurs.Add("Psitticosaurus");
dinosaurs.Add("Caudipteryx");
dinosaurs.Add("Compsognathus");
dinosaurs.Add("Muttaburrasaurus");
Display(dinosaurs);
Console.WriteLine("\nIndexOf(\"Muttaburrasaurus\"): {0}",
dinosaurs.IndexOf("Muttaburrasaurus"));
Console.WriteLine("\nContains(\"Caudipteryx\"): {0}",
dinosaurs.Contains("Caudipteryx"));
Console.WriteLine("\nInsert(2, \"Nanotyrannus\")");
dinosaurs.Insert(2, "Nanotyrannus");
Console.WriteLine("\ndinosaurs[2]: {0}", dinosaurs[2]);
Console.WriteLine("\ndinosaurs[2] = \"Microraptor\"");
dinosaurs[2] = "Microraptor";
Console.WriteLine("\nRemove(\"Microraptor\")");
dinosaurs.Remove("Microraptor");
Console.WriteLine("\nRemoveAt(0)");
dinosaurs.RemoveAt(0);
Display(dinosaurs);
}
private static void Display(Collection<string> cs)
{
Console.WriteLine();
foreach( string item in cs )
{
Console.WriteLine(item);
}
}
private static void ChangedHandler(object source,
DinosaursChangedEventArgs e)
{
if (e.ChangeType==ChangeType.Replaced)
{
Console.WriteLine("{0} was replaced with {1}", e.ChangedItem,
e.ReplacedWith);
}
else if(e.ChangeType==ChangeType.Cleared)
{
Console.WriteLine("The dinosaur list was cleared.");
}
else
{
Console.WriteLine("{0} was {1}.", e.ChangedItem, e.ChangeType);
}
}
}
/* This code example produces the following output:
Psitticosaurus was Added.
Caudipteryx was Added.
Compsognathus was Added.
Muttaburrasaurus was Added.
Psitticosaurus
Caudipteryx
Compsognathus
Muttaburrasaurus
IndexOf("Muttaburrasaurus"): 3
Contains("Caudipteryx"): True
Insert(2, "Nanotyrannus")
Nanotyrannus was Added.
dinosaurs[2]: Nanotyrannus
dinosaurs[2] = "Microraptor"
Nanotyrannus was replaced with Microraptor
Remove("Microraptor")
Microraptor was Removed.
RemoveAt(0)
Psitticosaurus was Removed.
Caudipteryx
Compsognathus
Muttaburrasaurus
*/
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Public Class Dinosaurs
Inherits Collection(Of String)
Public Event Changed As EventHandler(Of DinosaursChangedEventArgs)
Protected Overrides Sub InsertItem( _
ByVal index As Integer, ByVal newItem As String)
MyBase.InsertItem(index, newItem)
RaiseEvent Changed(Me, New DinosaursChangedEventArgs( _
ChangeType.Added, newItem, Nothing))
End Sub
Protected Overrides Sub SetItem(ByVal index As Integer, _
ByVal newItem As String)
Dim replaced As String = Items(index)
MyBase.SetItem(index, newItem)
RaiseEvent Changed(Me, New DinosaursChangedEventArgs( _
ChangeType.Replaced, replaced, newItem))
End Sub
Protected Overrides Sub RemoveItem(ByVal index As Integer)
Dim removedItem As String = Items(index)
MyBase.RemoveItem(index)
RaiseEvent Changed(Me, New DinosaursChangedEventArgs( _
ChangeType.Removed, removedItem, Nothing))
End Sub
Protected Overrides Sub ClearItems()
MyBase.ClearItems()
RaiseEvent Changed(Me, New DinosaursChangedEventArgs( _
ChangeType.Cleared, Nothing, Nothing))
End Sub
End Class
' Event argument for the Changed event.
'
Public Class DinosaursChangedEventArgs
Inherits EventArgs
Public ReadOnly ChangedItem As String
Public ReadOnly ChangeType As ChangeType
Public ReadOnly ReplacedWith As String
Public Sub New(ByVal change As ChangeType, ByVal item As String, _
ByVal replacement As String)
ChangeType = change
ChangedItem = item
ReplacedWith = replacement
End Sub
End Class
Public Enum ChangeType
Added
Removed
Replaced
Cleared
End Enum
Public Class Demo
Public Shared Sub Main()
Dim dinosaurs As New Dinosaurs
AddHandler dinosaurs.Changed, AddressOf ChangedHandler
dinosaurs.Add("Psitticosaurus")
dinosaurs.Add("Caudipteryx")
dinosaurs.Add("Compsognathus")
dinosaurs.Add("Muttaburrasaurus")
Display(dinosaurs)
Console.WriteLine(vbLf & "IndexOf(""Muttaburrasaurus""): {0}", _
dinosaurs.IndexOf("Muttaburrasaurus"))
Console.WriteLine(vbLf & "Contains(""Caudipteryx""): {0}", _
dinosaurs.Contains("Caudipteryx"))
Console.WriteLine(vbLf & "Insert(2, ""Nanotyrannus"")")
dinosaurs.Insert(2, "Nanotyrannus")
Console.WriteLine(vbLf & "dinosaurs(2): {0}", dinosaurs(2))
Console.WriteLine(vbLf & "dinosaurs(2) = ""Microraptor""")
dinosaurs(2) = "Microraptor"
Console.WriteLine(vbLf & "Remove(""Microraptor"")")
dinosaurs.Remove("Microraptor")
Console.WriteLine(vbLf & "RemoveAt(0)")
dinosaurs.RemoveAt(0)
Display(dinosaurs)
End Sub
Private Shared Sub Display(ByVal cs As Collection(Of String))
Console.WriteLine()
For Each item As String In cs
Console.WriteLine(item)
Next item
End Sub
Private Shared Sub ChangedHandler(ByVal source As Object, _
ByVal e As DinosaursChangedEventArgs)
If e.ChangeType = ChangeType.Replaced Then
Console.WriteLine("{0} was replaced with {1}", _
e.ChangedItem, e.ReplacedWith)
ElseIf e.ChangeType = ChangeType.Cleared Then
Console.WriteLine("The dinosaur list was cleared.")
Else
Console.WriteLine("{0} was {1}.", _
e.ChangedItem, e.ChangeType)
End If
End Sub
End Class
' This code example produces the following output:
'
'Psitticosaurus was Added.
'Caudipteryx was Added.
'Compsognathus was Added.
'Muttaburrasaurus was Added.
'
'Psitticosaurus
'Caudipteryx
'Compsognathus
'Muttaburrasaurus
'
'IndexOf("Muttaburrasaurus"): 3
'
'Contains("Caudipteryx"): True
'
'Insert(2, "Nanotyrannus")
'Nanotyrannus was Added.
'
'dinosaurs(2): Nanotyrannus
'
'dinosaurs(2) = "Microraptor"
'Nanotyrannus was replaced with Microraptor
'
'Remove("Microraptor")
'Microraptor was Removed.
'
'RemoveAt(0)
'Psitticosaurus was Removed.
'
'Caudipteryx
'Compsognathus
'Muttaburrasaurus
備註
Collection<T>您可以建立其中一個建構型別的實例來立即使用 類別;您只需要指定要包含在集合中的物件類型。 此外,您可以從任何建構類型衍生自己的集合類型,或從 Collection<T> 類別本身衍生泛型集合類型。
類別 Collection<T> 提供受保護的方法,可用來在新增和移除專案、清除集合或設定現有專案的值時自定義其行為。
大部分 Collection<T> 的物件都可以修改。 不過, Collection<T> 無法修改以唯讀 IList<T> 物件初始化的物件。 如您要此類別的唯讀版本,請參閱 ReadOnlyCollection<T> 。
此集合中的專案可以使用整數索引來存取。 此集合中的索引是以零起始。
Collection<T> 接受 null
作為參考型別的有效值,並允許重複的專案。
給繼承者的注意事項
提供這個基類,讓實作者更容易建立自定義集合。 鼓勵實作者擴充此基類,而不是建立自己的基類。
建構函式
Collection<T>() |
初始化 Collection<T> 類別的新執行個體,這個執行個體是空白的。 |
Collection<T>(IList<T>) |
初始化 Collection<T> 類別的新執行個體,將其當做指定清單的包裝函式。 |
屬性
Count |
取得 Collection<T> 中實際包含的項目數目。 |
Item[Int32] |
在指定的索引位置上取得或設定項目。 |
Items |
取得 IList<T> 周圍的 Collection<T> 包裝函式。 |
方法
Add(T) |
將物件加入至 Collection<T> 的末端。 |
Clear() |
移除 Collection<T> 中的所有項目。 |
ClearItems() |
移除 Collection<T> 中的所有項目。 |
Contains(T) |
判斷某項目是否在 Collection<T> 中。 |
CopyTo(T[], Int32) |
從目標陣列的指定索引開始,將整個 Collection<T> 複製到相容的一維 Array。 |
Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
GetEnumerator() |
傳回在 Collection<T> 中逐一查看的列舉值。 |
GetHashCode() |
做為預設雜湊函式。 (繼承來源 Object) |
GetType() |
取得目前執行個體的 Type。 (繼承來源 Object) |
IndexOf(T) |
搜尋指定的物件,並傳回整個 Collection<T> 中第一個出現之以零為起始的索引。 |
Insert(Int32, T) |
將項目插入至 Collection<T> 中指定的索引位置。 |
InsertItem(Int32, T) |
將項目插入至 Collection<T> 中指定的索引位置。 |
MemberwiseClone() |
建立目前 Object 的淺層複製。 (繼承來源 Object) |
Remove(T) |
從 Collection<T> 移除特定物件之第一個符合的元素。 |
RemoveAt(Int32) |
移除 Collection<T> 之指定索引處的項目。 |
RemoveItem(Int32) |
移除 Collection<T> 之指定索引處的項目。 |
SetItem(Int32, T) |
取代指定之索引處的項目。 |
ToString() |
傳回代表目前物件的字串。 (繼承來源 Object) |
明確介面實作
ICollection.CopyTo(Array, Int32) |
從特定的 ICollection 索引開始,將 Array 的項目複製到 Array。 |
ICollection.IsSynchronized |
取得值,這個值表示對 ICollection 的存取是否同步 (安全執行緒)。 |
ICollection.SyncRoot |
取得可用以同步存取 ICollection 的物件。 |
ICollection<T>.IsReadOnly |
取得值,指出 ICollection<T> 是否唯讀。 |
IEnumerable.GetEnumerator() |
傳回逐一查看集合的列舉值。 |
IList.Add(Object) |
將項目加入至 IList。 |
IList.Contains(Object) |
判斷 IList 是否包含特定值。 |
IList.IndexOf(Object) |
判斷 IList 中指定項目的索引。 |
IList.Insert(Int32, Object) |
將項目插入 IList 中指定的索引處。 |
IList.IsFixedSize |
取得值,指出 IList 是否有固定的大小。 |
IList.IsReadOnly |
取得值,指出 IList 是否唯讀。 |
IList.Item[Int32] |
在指定的索引位置上取得或設定項目。 |
IList.Remove(Object) |
從 IList 移除特定物件之第一個符合的元素。 |
擴充方法
適用於
執行緒安全性
Visual Basic 中的公用靜態 (Shared
) 此類型的成員是安全線程。 並非所有的執行個體成員都是安全執行緒。
Collection<T>可以同時支援多個讀取器,只要集合未修改即可。 即使如此,透過集合列舉本質上不是安全線程的程式。 若要確保列舉期間的執行緒安全性,您可以在整個列舉期間鎖定集合。 若要讓多重執行緒能夠存取集合以便進行讀取和寫入,您必須實作自己的同步處理。
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應