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
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物件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.
        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
        ' Since Visual Basic collections are reindexed automatically, 
        ' remove the first member on each iteration.
    Next count
End Sub



如果可能的話,您應該使用System.Collections.Generic命名空間System.Collections.Concurrent或命名空間中的泛型集合,而不Collection是 Visual Basic。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.

  • 逐一查看整個集合,其中每個 .。。下一個語句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.



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



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


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


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


依位置或索引鍵取得 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.


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


傳回值,指出 Visual Basic Collection 物件是否包含具有特定機碼的項目。Returns a value that indicates whether a Visual Basic Collection object contains an element with a specific key.


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

(繼承來源 Object)

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


作為預設雜湊函數。Serves as the default hash function.

(繼承來源 Object)

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

(繼承來源 Object)

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)

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


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


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

(繼承來源 Object)


ICollection.CopyTo(Array, Int32)

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


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


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


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


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


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


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


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


判斷 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.


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


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


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


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


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

ISerializable.GetObjectData(SerializationInfo, StreamingContext)

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



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


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


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


IEnumerable 轉換成 IQueryableConverts an IEnumerable to an IQueryable.