通过


Collection 类

定义

Visual Basic Collection 是一组可称为单元的有序项。

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
[System.Serializable]
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
[<System.Serializable>]
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
属性
实现

示例

以下示例创建 Collection 一个对象 names 和一个对话框,用户可以向集合添加对象(名称)。 然后,它会在集合中显示名称,最后清空该集合,而无需释放 Collection 对象本身。

若要查看其工作原理,请从“项目”菜单中选择“添加类”命令,并声明在模块级别调用instanceName的公共nameClass变量(类型Public instanceName)以保存每个实例的名称。 将默认名称保留为 nameClass。 将以下代码复制并粘贴到另一个模块的 “常规 ”部分,然后在另一个过程中以语句 classNamer 开头。 (此示例仅适用于支持类的主机应用程序。

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泛型集合,而不是 Visual BasicCollectionSystem.Collections.Concurrent 有关详细信息,请参阅 集合 - C#集合 - Visual Basic

Visual Basic Collection 对象提供了一种将相关项组作为单个对象引用的便捷方法。 集合中的项或 元素只需与集合中存在的事实相关。 集合的元素不必共享相同的数据类型。

可以按照创建其他对象的方式创建集合,如以下示例所示。

Dim coll As New Microsoft.VisualBasic.Collection()

创建集合后,可以执行以下任一操作:

  • 使用 Add 该方法添加元素。

  • 使用该方法删除元素 Remove

  • 使用 Clear 该方法删除所有元素。

  • 了解集合包含 Count 的属性包含的元素数。

  • 检查方法中是否存在特定元素 Contains

  • 使用 Item[] 属性返回集合中的特定元素。

  • 使用 For Each 循环访问整个集合 ...Next 语句

    注释

    尽管 Visual Basic 对象的功能与 Collection Visual Basic Collection 6.0 中的对象相同,但两者无法在 COM 环境中互操作。

    注意

    循环访问 Visual Basic Collection 不是线程安全的过程。 即使集合已同步,其他线程仍可以修改集合,从而导致枚举器引发异常。 若要保证枚举期间的线程安全性,请锁定集合或捕获由其他线程所做的更改导致的异常。 有关锁定编程元素的详细信息,请参阅 SyncLock 语句

构造函数

名称 说明
Collection()

创建并返回新的 Visual Basic Collection 对象。

属性

名称 说明
Count

获取集合中的元素数。

Item[Int32]

按位置或键获取对象的特定元素 Collection

Item[Object]

按位置或键获取对象的特定元素 Collection

Item[String]

按位置或键获取对象的特定元素 Collection

方法

名称 说明
Add(Object, String, Object, Object)

将元素添加到 Collection 对象。

Clear()

删除 Visual Basic Collection 对象的所有元素。

Contains(String)

返回一个值,该值指示 Visual Basic Collection 对象是否包含具有特定键的元素。

Equals(Object)

确定指定的对象是否等于当前对象。

(继承自 Object)
GetEnumerator()

返回循环访问集合的枚举器。

GetHashCode()

用作默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
Remove(Int32)

Collection 对象中删除元素。

Remove(String)

Collection 对象中删除元素。

ToString()

返回一个表示当前对象的字符串。

(继承自 Object)

显式接口实现

名称 说明
ICollection.CopyTo(Array, Int32)

将元素Collection复制到从特定Array索引开始的元素Array

ICollection.Count

获取此集合中的项数。

ICollection.IsSynchronized

获取一个值,该值指示是否同步对 Collection 对象的访问(线程安全)。

ICollection.SyncRoot

获取一个对象,该对象可用于同步对 Collection 对象的访问。

IDeserializationCallback.OnDeserialization(Object)

在反序列化整个 Collection 对象图后运行。

IEnumerable.GetEnumerator()

获取循环访问集合的枚举器。

IList.Add(Object)

将项添加到集合。

IList.Clear()

从对象中删除所有项 Collection

IList.Contains(Object)

确定对象是否 Collection 包含特定值。

IList.IndexOf(Object)

确定对象中 Collection 特定项的索引。

IList.Insert(Int32, Object)

将项插入到 Collection 指定索引处的对象。

IList.IsFixedSize

获取一个值,该值指示对象是否 Collection 具有固定大小。

IList.IsReadOnly

获取一个值,该值指示对象是否 Collection 为。

IList.Item[Int32]

获取或设置指定索引处的元素。

IList.Remove(Object)

从对象中删除特定对象的 Collection 第一个匹配项。

IList.RemoveAt(Int32)

Collection删除指定索引处的对象项。

ISerializable.GetObjectData(SerializationInfo, StreamingContext)

返回序列化 Collection 对象所需的数据。

扩展方法

名称 说明
AsParallel(IEnumerable)

启用查询的并行化。

AsQueryable(IEnumerable)

IEnumerable 转换为 IQueryable

Cast<TResult>(IEnumerable)

IEnumerable 的元素强制转换为指定类型。

OfType<TResult>(IEnumerable)

根据指定类型筛选 IEnumerable 的元素。

适用于