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, System::Runtime::Serialization::IDeserializationCallback, System::Runtime::Serialization::ISerializable
[System.Serializable]
public sealed class Collection : System.Collections.IList, System.Runtime.Serialization.IDeserializationCallback, System.Runtime.Serialization.ISerializable
type Collection = class
    interface ICollection
    interface IList
    interface ISerializable
    interface IDeserializationCallback
Public NotInheritable Class Collection
Implements IDeserializationCallback, IList, ISerializable
继承
Collection
属性
实现

示例

下面的示例创建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.
    Do
        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
        names.Remove(1)
        ' 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. 有关详细信息,请参阅集合For more information, see Collections.

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:

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

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

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

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

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

  • 使用Item[String]属性返回集合中的特定元素。Return a specific element from the collection with the Item[String] 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.

构造函数

Collection()

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

属性

Count

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

Item[Int32]

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

Item[Object]

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

Item[String]

按位置或键获取 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.

Clear()

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

Contains(String)

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

Equals(Object)

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

(继承自 Object)
GetEnumerator()

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

GetHashCode()

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

(继承自 Object)
GetType()

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

(继承自 Object)
MemberwiseClone()

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

(继承自 Object)
Remove(Int32)

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

Remove(String)

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

ToString()

返回表示当前对象的字符串。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.

ICollection.Count

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

ICollection.IsSynchronized

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

ICollection.SyncRoot

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

IDeserializationCallback.OnDeserialization(Object)

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

IEnumerable.GetEnumerator()

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

IList.Add(Object)
IList.Clear()

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

IList.Contains(Object)

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

IList.IndexOf(Object)

确定 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.

IList.IsFixedSize

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

IList.IsReadOnly

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

IList.Item[Int32]

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

IList.Remove(Object)

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

IList.RemoveAt(Int32)

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

ISerializable.GetObjectData(SerializationInfo, StreamingContext)

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

扩展方法

Cast<TResult>(IEnumerable)

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

OfType<TResult>(IEnumerable)

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

AsParallel(IEnumerable)

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

AsQueryable(IEnumerable)

IEnumerable 转换为 IQueryableConverts an IEnumerable to an IQueryable.

适用于