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 物件本身的情況下清空集合。
要了解運作方式,請從專案選單選擇 Add Class 指令,並在模組層nameClass級(類型Public instanceName)宣告一個公開變數instanceName,以儲存每個實例的名稱。 預設名稱保持為 nameClass。 將以下程式碼複製並貼上到另一個模組的 General 區段,然後在另一個程序中以該語句 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 通用集合,或是命名空間中的 System.Collections.Concurrent 通用集合,而不是 Visual Basic Collection。 欲了解更多資訊,請參閱 Collections - C# 或 Collections - Visual Basic。
Visual Basic Collection 物件提供了一種方便的方式,將相關項目群組視為單一物件。 集合中的項目或 元素只需透過它們存在於集合中而產生關聯。 集合中的元素不必共享相同的資料型態。
你可以像建立其他物件一樣建立集合,如下範例所示。
Dim coll As New Microsoft.VisualBasic.Collection()
建立收藏後,你可以做以下任一:
在方法 Add 中加入一個元素。
用方法 Remove 去除一個元素。
用這個 Clear 方法去除所有元素。
了解該集合包含多少元素。Count
檢查該方法是否包含 Contains 特定元素。
從集合中回傳帶有屬性 Item[] 的特定元素。
-
備註
雖然 Visual Basic
Collection物件的功能與CollectionVisual Basic 6.0 中的物件相同,但兩者在 COM 環境中無法互通。謹慎
透過 Visual Basic
Collection迭代並非執行緒安全的程序。 即使集合已同步,其他執行緒仍可能修改集合,導致列舉器拋出例外。 為了確保列舉時執行緒的安全,要麼鎖定集合,要麼攔截其他執行緒變更所產生的異常。 欲了解更多關於鎖定程式元素的資訊,請參閱 SyncLock 陳述式。
建構函式
| 名稱 | Description |
|---|---|
| Collection() |
建立並回傳一個新的 Visual Basic Collection 物件。 |
屬性
| 名稱 | Description |
|---|---|
| Count |
取得集合中元素的數量。 |
| Item[Int32] |
透過位置或鍵位獲得物件的特定 |
| Item[Object] |
透過位置或鍵位獲得物件的特定 |
| Item[String] |
透過位置或鍵位獲得物件的特定 |
方法
| 名稱 | Description |
|---|---|
| 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) |
明確介面實作
| 名稱 | Description |
|---|---|
| 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 物件所需的資料。 |
擴充方法
| 名稱 | Description |
|---|---|
| AsParallel(IEnumerable) |
啟用查詢的平行處理。 |
| AsQueryable(IEnumerable) |
將 IEnumerable 轉換成 IQueryable。 |
| Cast<TResult>(IEnumerable) |
將 IEnumerable 的項目轉換成指定的型別。 |
| OfType<TResult>(IEnumerable) |
根據指定的型別篩選 IEnumerable 的專案。 |