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# 中的索引器)检索和设置字符串。 该示例使用 Remove 方法和索引使用 RemoveAt 方法按字符串标识删除字符串。 最后,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 方法,以便为 Add、Insert、Remove和 Clear 方法以及设置 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> 对象都可以修改。 但是,不能修改使用只读 IList<T> 对象初始化的 Collection<T> 对象。 有关此类的只读版本,请参阅 ReadOnlyCollection<T>。
可以使用整数索引访问此集合中的元素。 此集合中的索引从零开始。
Collection<T> 接受 null
作为引用类型的有效值,并允许重复元素。
继承者说明
提供了此基类,以便实现者更轻松地创建自定义集合。 鼓励实现者扩展此基类,而不是创建自己的基类。
构造函数
Collection<T>() |
初始化 Collection<T> 类的新实例,该实例为空。 |
Collection<T>(IList<T>) |
将 Collection<T> 类的新实例初始化为指定列表的包装器。 |
属性
Count |
获取实际包含在 Collection<T>中的元素数。 |
Item[Int32] |
获取或设置指定索引处的元素。 |
Items |
获取 Collection<T>周围的 IList<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) |
从特定 Array 索引开始,将 ICollection 的元素复制到 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中删除特定对象的第一个匹配项。 |
扩展方法
适用于
线程安全性
此类型的公共静态(Shared
)成员是线程安全的。 不保证任何实例成员都是线程安全的。
只要集合未修改,Collection<T> 就可以同时支持多个读取器。 即便如此,通过集合进行枚举本质上不是线程安全的过程。 若要保证枚举期间的线程安全性,可以在整个枚举期间锁定集合。 若要允许多个线程访问集合进行读取和写入,必须实现自己的同步。
另请参阅
- ICollection<T>
- 在集合 中执行 Culture-Insensitive 字符串操作