Collection 类


Visual Basic Collection 是可作为单元引用的一组有序项。A Visual Basic Collection is an ordered set of items that can be referred to as a unit.

public ref class Collection sealed : System::Collections::IList
public ref class Collection sealed : System::Collections::IList, System::Runtime::Serialization::IDeserializationCallback, System::Runtime::Serialization::ISerializable
public sealed class Collection : System.Collections.IList
public sealed class Collection : System.Collections.IList, System.Runtime.Serialization.IDeserializationCallback, System.Runtime.Serialization.ISerializable
type Collection = class
    interface ICollection
    interface IEnumerable
    interface IList
type Collection = class
    interface ICollection
    interface IList
type Collection = class
    interface ICollection
    interface IList
    interface ISerializable
    interface IDeserializationCallback
Public NotInheritable Class Collection
Implements IList
Public NotInheritable Class Collection
Implements IDeserializationCallback, IList, ISerializable


下面的示例创建 Collection 对象 names 和一个对话框,用户可以使用该对话框将 (名称) 对象添加到集合中。The following example creates the Collection object names and a dialog box with which a user can add objects (names) to the collection. 然后,它显示集合中的名称,最后清空集合,而不释放 Collection 对象本身。It then displays the names in the collection, and finally empties the collection without disposing of the Collection object itself.

若要查看其工作原理,请从 "项目" 菜单中选择 "添加类" 命令,然后在 instanceName " (类型") 的模块级别声明一个名为的公共变量, nameClass Public instanceName 以便保存每个实例的名称。To see how this works, choose the Add Class command from the Project menu and declare a public variable called instanceName at the module level of nameClass (type Public instanceName) to hold the names of each instance. 保留默认名称为 nameClassLeave the default name as nameClass. 将以下代码复制并粘贴到另一个模块的 " 常规 " 部分,然后用 classNamer 另一个过程中的语句启动它。Copy and paste the following code into the General section of another module, and then start it with the statement classNamer in another procedure. (此示例仅适用于支持类的主机应用程序。 ) (This example works only with host applications that support classes.)

Public Class nameClass
    Public instanceName As String
End Class
Sub classNamer()
    ' Create a Visual Basic Collection object.
    Dim names As New Microsoft.VisualBasic.Collection()
    Dim key As Integer
    Dim msg As String
    Dim name As String
    Dim nameList As String = ""
    ' 1. Get names from the user to add to the collection.
        Dim inst As New nameClass()
        key += 1
        msg = "Please enter a name for this object." & vbCrLf &
              "Press Cancel to see names in collection."
        name = InputBox(msg, "Name the Collection items")
        inst.instanceName = name
        ' If user entered a name, add it to the collection.
        If inst.instanceName <> "" Then
            names.Add(inst, CStr(key))
        End If
    Loop Until name = ""
    ' 2. Create and display a list of names from the collection.
    For Each oneInst As nameClass In names
        nameList &= oneInst.instanceName & vbCrLf
    Next oneInst
    MsgBox(nameList, , "Instance Names in names Collection")
    ' 3. Remove elements from the collection without disposing of the collection.
    For count As Integer = 1 To names.Count
        ' Since Visual Basic collections are reindexed automatically, 
        ' remove the first member on each iteration.
    Next count
End Sub



只要可能,您应使用 System.Collections.Generic 命名空间或 System.Collections.Concurrent 命名空间而不是 Visual Basic Collection 中的泛型集合。Whenever possible, you should use the generic collections in the System.Collections.Generic namespace or the System.Collections.Concurrent namespace instead of a Visual Basic Collection. 有关详细信息,请参阅 集合-c #集合-Visual BasicFor more information, see Collections - C# or Collections - Visual Basic.

Visual Basic Collection 对象提供了一种将一组相关项作为单个对象来引用的便捷方法。The Visual Basic Collection object provides a convenient way to refer to a related group of items as a single object. 集合中的项或 元素 只需要与集合中存在的项相关联。The items, or elements, in a collection need only be related by the fact that they exist in the collection. 集合的元素不必共享同一数据类型。Elements of a collection do not have to share the same data type.

您可以使用与创建其他对象相同的方式来创建集合,如下面的示例所示。You can create a collection the same way you create other objects, as the following example illustrates.

Dim coll As New Microsoft.VisualBasic.Collection()  

创建集合后,可以执行以下任一操作:Once you have created a collection, you can do any of the following:

  • 使用方法添加元素 AddAdd an element with the Add method.

  • 使用方法移除元素 RemoveRemove an element with the Remove method.

  • 删除包含方法的所有元素 ClearRemove all elements with the Clear method.

  • 了解集合包含的元素与属性的数目 CountFind out how many elements the collection contains with the Count property.

  • 使用方法检查特定元素是否存在 ContainsCheck whether a specific element is present with the Contains method.

  • 使用属性返回集合中的特定元素 Item[]Return a specific element from the collection with the Item[] property.

  • 使用 For Each 循环访问整个集合 下一语句Iterate through the entire collection with the For Each...Next Statement.


    尽管 Visual Basic Collection 对象具有与 Visual Basic 6.0 中的对象相同的功能 Collection ,但这两种功能无法在 COM 环境中进行互操作。Although the Visual Basic Collection object has functionality identical to the Collection object in Visual Basic 6.0, the two cannot interoperate in a COM environment.


    循环访问 Visual Basic Collection 不是一个线程安全的过程。Iterating through a Visual Basic Collection is not a thread-safe procedure. 即使集合已同步,其他线程仍可以修改该集合,从而导致枚举器引发异常。Even if the collection is synchronized, other threads can still modify the collection, causing the enumerator to throw an exception. 若要保证枚举过程中的线程安全,请锁定集合或捕获由其他线程所做的更改所导致的异常。To guarantee thread safety during enumeration, either lock the collection or catch the exceptions resulting from changes made by other threads. 有关锁定编程元素的详细信息,请参阅 SyncLock 语句For more information on locking a programming element, see SyncLock Statement.



创建并返回一个新的 Visual Basic Collection 对象。Creates and returns a new Visual Basic Collection object.



获取集合中的元素数。Gets the number of elements in a collection.


按位置或键获取 Collection 对象的特定元素。Gets a specific element of a Collection object either by position or by key.


按位置或键获取 Collection 对象的特定元素。Gets a specific element of a Collection object either by position or by key.


按位置或键获取 Collection 对象的特定元素。Gets a specific element of a Collection object either by position or by key.


Add(Object, String, Object, Object)

将一个元素添加到 Collection 对象。Adds an element to a Collection object.


删除 Visual Basic Collection 对象的所有元素。Deletes all elements of a Visual Basic Collection object.


返回一个值,该值指示 Visual Basic Collection 对象是否包含一个带有特定键的元素。Returns a value that indicates whether a Visual Basic Collection object contains an element with a specific key.


确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)

返回一个循环访问集合的枚举器。Returns an enumerator that iterates through the collection.


作为默认哈希函数。Serves as the default hash function.

(继承自 Object)

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)

Collection 对象移除元素。Removes an element from a Collection object.


Collection 对象移除元素。Removes an element from a Collection object.


返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)


ICollection.CopyTo(Array, Int32)

从特定的 Collection 索引开始,将 Array 的元素复制到一个 Array 中。Copies the elements of the Collection to an Array, starting at a particular Array index.


获取此集合中的项数。Gets the number of items in this collection.


获取一个值,该值指示对 Collection 对象的访问是否已同步(线程安全)。Gets a value that indicates whether access to the Collection object is synchronized (thread-safe).


获取一个对象,该对象可用于同步对 Collection 对象的访问。Gets an object that can be used to synchronize access to the Collection object.


在整个 Collection 对象图反序列化后运行。Runs after the entire Collection object graph has been deserialized.


获取能够循环访问集合的枚举器。Gets an enumerator that iterates through the collection.


向集合中添加一项。Adds an item to the collection.


Collection 对象中移除所有项。Removes all items from the Collection object.


确定 Collection 对象是否包含特定值。Determines whether the Collection object contains a specific value.


确定 Collection 对象中特定项的索引。Determines the index of a specific item in the Collection object.

IList.Insert(Int32, Object)

将项插入 Collection 对象中的指定索引处。Inserts an item to the Collection object at the specified index.


获取一个值,该值指示 Collection 对象是否具有固定大小。Gets a value that indicates whether the Collection object has a fixed size.


获取一个值,它指示 Collection 对象是否只读。Gets a value that indicates whether the Collection object is.


获取或设置指定索引处的元素。Gets or sets the element at the specified index.


Collection 对象中移除特定对象的第一个匹配项。Removes the first occurrence of a specific object from the Collection object.


移除指定索引处的 Collection 对象项。Removes the Collection object item at the specified index.

ISerializable.GetObjectData(SerializationInfo, StreamingContext)

返回对 Collection 对象进行序列化所需的数据。Returns the data needed to serialize the Collection object.



IEnumerable 的元素强制转换为指定的类型。Casts the elements of an IEnumerable to the specified type.


根据指定类型筛选 IEnumerable 的元素。Filters the elements of an IEnumerable based on a specified type.


启用查询的并行化。Enables parallelization of a query.


IEnumerable 转换为 IQueryableConverts an IEnumerable to an IQueryable.