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 对象本身。

若要查看工作原理,请从 Project 菜单中选择 “添加类”命令,并在 (类型的Public instanceName模块级别nameClass声明一个公共变量instanceName,) 以保存每个实例的名称。 将默认名称保留为 nameClass. 将以下代码复制并粘贴到另一个模块的 “常规 ”部分,然后在另一过程中用语句 classNamer 启动它。 (此示例仅适用于支持 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 中的泛型集合。 有关详细信息,请参阅集合 - C#集合 - Visual Basic

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

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

Dim coll As New Microsoft.VisualBasic.Collection()  

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

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

  • 使用 Remove 该方法删除元素。

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

  • 了解集合包含 Count 的属性有多少个元素。

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

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

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

    备注

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

    注意

    循环访问Visual BasicCollection不是线程安全的过程。 即使集合已同步,其他线程仍可以修改集合,从而导致枚举器引发异常。 若要保证枚举期间的线程安全性,请锁定集合或捕获由其他线程所做的更改导致的异常。 有关锁定编程元素的详细信息,请参阅 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 对象进行序列化所需的数据。

扩展方法

Cast<TResult>(IEnumerable)

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

OfType<TResult>(IEnumerable)

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

AsParallel(IEnumerable)

启用查询的并行化。

AsQueryable(IEnumerable)

IEnumerable 转换为 IQueryable

适用于