Visual Basic Collections

In visual basic, collection is a class which is useful to manage a group of objects in a flexible manner to perform various operations like insert, update, delete, get, etc. on object items in a dynamic manner based on our requirements.

 

Generally, while working in visual basic applications we will get a requirement like to create or manage a group of related objects. In that case, we have two ways to create a group objects in visual basic i.e. by using arrays and collections.

 

In the previous section, we learned about arrays in vb but those are useful only when we are working with a fixed number of strongly-typed objects. So, to solve this problem Microsoft has introduced collections in visual basic to work with a group of objects which can grow or shrink dynamically based on our requirements.

 

In visual basic, the collection is a class so we must need to declare an instance of the class before we perform any operations like add, delete, etc. on the defined collection and the collections are implemented by using IEnumerable interface so we can access collection items by using a foreach loop.

Visual Basic Collection Types

In visual basic, we have a different type of collection classes are available, those are

 

  • Non-Generic (System.Collections)
  • Generic (System.Collections.Generic)
  • Concurrent (System.Collections.Concurrent)

Visual Basic Non-Generic Collections

In visual basic, non-generic collection classes are useful to store elements of different data types and these are provided by System.Collections namespace. Now, these collection classes are legacy types so whenever possible try to use generic collections (System.Collections.Generic) or concurrent collections (System.Collections.Concurrent).

 

Following are the different type of non-generic collection classes which are provided by System.Collections namespace.

 

ClassDescription
ArrayList It is useful to represent an array of objects whose size is dynamically increased as required.
Queue It is useful to represent a FIFO (First in, First Out) collection of objects.
Stack It is useful to represent a LIFO (Last in, First Out) collection of objects.
Hashtable It is useful to represent a collection of key/value pairs that are organized based on the hash code of the key.

Visual Basic Generic Collections

In visual basic, generic collections will enforce a type-safety so we can store only the elements which are having the same data type and these are provided by System.Collections.Generic namespace.

 

Following are the different type of generic collection classes which are provided by System.Collections.Generic namespace.

 

ClassDescription
List It is useful to represent a list of objects that can be accessed by index.
Queue It is useful to represent a FIFO (First in, First Out) collection of objects.
Stack It is useful to represent a LIFO (Last in, First Out) collection of objects.
SortedList<K,V> It is useful to represent a collection of key/value pairs that are sorted by a key.
Dictionary<K,V> It is useful to represent a collection of key/value pairs that are organized based on the key.

Visual Basic Concurrent Collections

In visual basic, concurrent collections are useful to access collection items from multiple threads and these are available from .NET Framework 4 with System.Collections.Concurrent namespace.

 

In case, if we are using multiple threads to access a collection concurrently, then we need to use concurrent collections instead of non-generic and generic collections.

 

Following are the different type of concurrent collection classes which are provided by System.Collections.Concurrent namespace.

 

ClassDescription
BlockingCollection It is useful to provide a blocking and bounding capabilities for thread-safe collections.
ConcurrentBag It is useful to represent a thread-safe, unordered collection of objects.
ConcurrentDictionary<K,V> It is useful to represent a thread-safe collection of key/value pairs that can be accessed by multiple threads concurrently.
ConcurrentQueue It is useful to represent a thread-safe FIFO (First in, First Out) collection.
ConcurrentStack It is useful to represent a thread-safe LIFO (Last in, First Out) collection.
Partitioner It is useful to provide a partitioning strategies for arrays, lists, enumerables.
OrderablePartitioner It is useful to provide a specific way of splitting an orderable data source into multiple partitions.

Topics Covered