Collection 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
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 对象本身。
若要查看其工作原理,请从“项目”菜单中选择“添加类”命令,并在模块级别的 nameClass (类型 Public instanceName) 声明一个名为 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 Basic Collection 对象提供了一种将相关项组作为单个对象引用的便捷方法。 集合中的项或 元素只需与集合中存在的事实相关。 集合的元素不必共享相同的数据类型。
可以像创建其他对象一样创建集合,如以下示例所示。
Dim coll As New Microsoft.VisualBasic.Collection()
创建集合后,可以执行以下任一操作:
使用 Add 方法添加元素。
使用 Remove 方法删除元素。
使用 Clear 方法删除所有元素。
使用 属性了解集合包含 Count 的元素数。
检查 方法中 Contains 是否存在特定元素。
使用 Item[] 属性从集合中返回特定元素。
使用 For Each 循环访问整个集合...Next 语句。
注意
尽管 Visual Basic 对象的功能与 Visual Basic
Collection6.0 中的 对象相同Collection,但两者无法在 COM 环境中互操作。注意
循环访问 Visual Basic
Collection不是线程安全的过程。 即使集合已同步,其他线程仍可修改集合,导致枚举器引发异常。 若要在枚举期间保证线程安全,请锁定集合或捕获由其他线程所做的更改导致的异常。 有关锁定编程元素的详细信息,请参阅 SyncLock 语句。
构造函数
| Collection() |
创建并返回一个新的 Visual Basic Collection 对象。 |
属性
| Count |
获取集合中的元素数。 |
| Item[Int32] |
按位置或键获取 |
| Item[Object] |
按位置或键获取 |
| Item[String] |
按位置或键获取 |
方法
| Add(Object, String, Object, Object) |
将一个元素添加到 |
| Clear() |
删除 Visual Basic |
| Contains(String) |
返回一个值,该值指示 Visual Basic |
| Equals(Object) |
确定指定对象是否等于当前对象。 (继承自 Object) |
| GetEnumerator() |
返回一个循环访问集合的枚举器。 |
| GetHashCode() |
作为默认哈希函数。 (继承自 Object) |
| GetType() |
获取当前实例的 Type。 (继承自 Object) |
| MemberwiseClone() |
创建当前 Object 的浅表副本。 (继承自 Object) |
| Remove(Int32) |
从 |
| Remove(String) |
从 |
| 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。 |