Collection Collection Collection Collection Class

定義

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
繼承
CollectionCollectionCollectionCollection
屬性
實作

範例

下列範例會建立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 CollectionWhenever 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 BasicCollection物件提供便利的方式,來參考的項目為單一物件相關的群組。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.

  • 逐一查看整個集合與每個...下一個陳述式Iterate through the entire collection with the For Each...Next Statement.

    注意

    雖然 Visual BasicCollection物件具有相同的功能Collection物件在 Visual Basic 6.0 中,這兩個無法在 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 BasicCollection不是安全執行緒程序。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() Collection() Collection() Collection()

建立並傳回新的Visual Basic Collection 物件。Creates and returns a new Visual Basic Collection object.

屬性

Count Count Count Count

取得集合中的元素數目。Gets the number of elements in a collection.

Item[Int32] Item[Int32] Item[Int32] Item[Int32]

依位置或索引鍵取得 Collection 物件的特定元素。Gets a specific element of a Collection object either by position or by key.

Item[Object] Item[Object] Item[Object] Item[Object]

依位置或索引鍵取得 Collection 物件的特定元素。Gets a specific element of a Collection object either by position or by key.

Item[String] Item[String] Item[String] Item[String]

依位置或索引鍵取得 Collection 物件的特定元素。Gets a specific element of a Collection object either by position or by key.

方法

Add(Object, String, Object, Object) Add(Object, String, Object, Object) Add(Object, String, Object, Object) Add(Object, String, Object, Object)

將項目加入至 Collection 物件。Adds an element to a Collection object.

Clear() Clear() Clear() Clear()

刪除 Visual Basic Collection 物件的所有項目。Deletes all elements of a Visual Basic Collection object.

Contains(String) Contains(String) Contains(String) 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) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetEnumerator() GetEnumerator() GetEnumerator() GetEnumerator()

傳回逐一查看集合的列舉值。Returns an enumerator that iterates through the collection.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
Remove(Int32) Remove(Int32) Remove(Int32) Remove(Int32)

Collection 物件移除項目。Removes an element from a Collection object.

Remove(String) Remove(String) Remove(String) Remove(String)

Collection 物件移除項目。Removes an element from a Collection object.

ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)

明確介面實作

ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32)

從特定的 Collection 索引開始,將 Array 的項目複製到 ArrayCopies the elements of the Collection to an Array, starting at a particular Array index.

ICollection.Count ICollection.Count ICollection.Count ICollection.Count

取得此集合中的項目數。Gets the number of items in this collection.

ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized

取得值,指出是否同步處理 Collection 物件的存取權 (安全執行緒)。Gets a value that indicates whether access to the Collection object is synchronized (thread-safe).

ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot

取得可用來同步處理對 Collection 物件之存取的物件。Gets an object that can be used to synchronize access to the Collection object.

IDeserializationCallback.OnDeserialization(Object) IDeserializationCallback.OnDeserialization(Object) IDeserializationCallback.OnDeserialization(Object) IDeserializationCallback.OnDeserialization(Object)

在整個 Collection 物件圖形還原序列化之後執行。Runs after the entire Collection object graph has been deserialized.

IEnumerable.GetEnumerator() IEnumerable.GetEnumerator() IEnumerable.GetEnumerator() IEnumerable.GetEnumerator()

取得可逐一查看集合的列舉值。Gets an enumerator that iterates through the collection.

IList.Add(Object) IList.Add(Object) IList.Add(Object) IList.Add(Object)
IList.Clear() IList.Clear() IList.Clear() IList.Clear()

將所有項目從 Collection 物件中移除。Removes all items from the Collection object.

IList.Contains(Object) IList.Contains(Object) IList.Contains(Object) IList.Contains(Object)

判斷 Collection 物件是否包含特定的值。Determines whether the Collection object contains a specific value.

IList.IndexOf(Object) IList.IndexOf(Object) IList.IndexOf(Object) IList.IndexOf(Object)

判斷 Collection 物件中特定項目的索引。Determines the index of a specific item in the Collection object.

IList.Insert(Int32, Object) IList.Insert(Int32, Object) IList.Insert(Int32, Object) IList.Insert(Int32, Object)

將項目插入至指定之索引的 Collection 物件。Inserts an item to the Collection object at the specified index.

IList.IsFixedSize IList.IsFixedSize IList.IsFixedSize IList.IsFixedSize

取得值,這個值會指出 Collection 物件的大小是否固定。Gets a value that indicates whether the Collection object has a fixed size.

IList.IsReadOnly IList.IsReadOnly IList.IsReadOnly IList.IsReadOnly

取得值,指出 Collection 物件是否為。Gets a value that indicates whether the Collection object is.

IList.Item[Int32] IList.Item[Int32] IList.Item[Int32] IList.Item[Int32]

在指定的索引位置上取得或設定項目。Gets or sets the element at the specified index.

IList.Remove(Object) IList.Remove(Object) IList.Remove(Object) IList.Remove(Object)

Collection 物件中移除第一次出現的特定物件。Removes the first occurrence of a specific object from the Collection object.

IList.RemoveAt(Int32) IList.RemoveAt(Int32) IList.RemoveAt(Int32) IList.RemoveAt(Int32)

移除指定之索引的 Collection 物件項目。Removes the Collection object item at the specified index.

ISerializable.GetObjectData(SerializationInfo, StreamingContext) ISerializable.GetObjectData(SerializationInfo, StreamingContext) ISerializable.GetObjectData(SerializationInfo, StreamingContext) ISerializable.GetObjectData(SerializationInfo, StreamingContext)

傳回序列化 Collection 物件所需的資料。Returns the data needed to serialize the Collection object.

Extension Methods

Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable)

根據指定的型別來篩選 IEnumerable 的項目。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable)

啟用查詢的平行化作業。Enables parallelization of a query.

AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable)

將轉換IEnumerableIQueryableConverts an IEnumerable to an IQueryable.

適用於