In many applications, you will need to manage groups of related objects. There are two ways to group objects: creating arrays of objects and creating collections of objects.
For some collections, you can assign a key to any object that you put into the collection to quickly retrieve the object by using the key.
A collection is a class, so you must declare an instance of the class before adding elements to that collection.
Many common collections are provided by the .NET Framework. Each type of collection is designed for a specific purpose.
Some of the common collection classes are described in this section:
You can create a generic collection by using one of the classes in the System.Collections.Generic
namespace.
The following table lists some of the frequently used classes of the System.Collections.Generic namespace:
Class | Description |
---|---|
Dictionary<TKey,TValue> | Represents a collection of key/value pairs that are organized based on the key. |
List |
Represents a list of objects that can be accessed by index. Provides methods to search, sort, and modify lists. |
Queue |
Represents a first-in, first-out (FIFO) collection of objects. |
SortedList<TKey,TValue> | Represents a collection of key/value pairs sorted by key based on the associated IComparer |
Stack |
Represents a last-in, first-out (LIFO) collection of objects. |
The following example shows how to create and initialize a List
and how to display its values.
Dim customers As List(Of Customer) = New List(Of Customer)()
customers.Add(New Customer With {
.Id = 1,
.Name = "John"
})
customers.Add(New Customer With {
.Id = 2,
.Name = "Mark"
})
customers.Add(New Customer With {
.Id = 3,
.Name = "Stella"
})
For Each customer In customers
Console.WriteLine("{0}, {1}", customer.Id, customer.Name)
Next
The classes in the System.Collections
namespace do not store elements as specifically typed objects but as objects of type Object
.
The following table lists some of the frequently used classes in the System.Collections
namespace:
Class | Description |
---|---|
ArrayList | Represents an array of objects whose size is dynamically increased as required. |
Hashtable | Represents a collection of key/value pairs organized based on the hash code of the key. |
Queue | Represents a first-in, first-out (FIFO) collection of objects. |
Stack | Represents a last-in, first-out (LIFO) collection of objects. |
The following example shows how to create and initialize an ArrayList
and how to display its values.
Dim myAL As ArrayList = New ArrayList()
myAL.Add("Hello")
myAL.Add("World")
myAL.Add("!")
Console.WriteLine("myAL")
Console.WriteLine(" Count: {0}", myAL.Count)
Console.WriteLine(" Capacity: {0}", myAL.Capacity)
Console.Write(" Values:")
For Each obj As Object In myAL
Console.Write(" {0}", obj)
Next
The collections in the System.Collections.Concurrent
namespace provides efficient thread-safe operations for accessing collection items from multiple threads.
System.Collections.Concurrent
namespace should be used instead of the corresponding types in the System.Collections.Generic
and System.Collections
namespaces whenever multiple threads are accessing the collection concurrently.System.Collections.Concurrent
namespace are BlockingCollection<T>
, ConcurrentDictionary<TKey,TValue>
, ConcurrentQueue<T>
, and ConcurrentStack<T>
.A Visual Basic Collection is an ordered set of items that can be referred to as a unit. It can be used to access a collection item by using either a numeric index or a String key.
Object
, so you can add an item of any data type.The following example shows how to create and initialize a Microsoft.VisualBasic.Collection
and how to display its values.
Dim customers As New Microsoft.VisualBasic.Collection()
customers.Add(New Customer With {
.Id = 1,
.Name = "John"
}, "1")
customers.Add(New Customer With {
.Id = 2,
.Name = "Mark"
}, "2")
customers.Add(New Customer With {
.Id = 3,
.Name = "Stella"
}, "3")
For Each customer In customers
Console.WriteLine("{0}, {1}", customer.Id, customer.Name)
Next
Whenever possible, you should use the generic collections in the System.Collections.Generic namespace or the System.Collections.Concurrent namespace instead of the Visual Basic Collection class.