Visual Basic .NET/Collections

VB.NET supports a number of data structures called Collections. Collection can be either zero or one based, depending on how they are declared. A collection is similar to an array in that it can store multiple data entries. However, most collections have advanced methods associated with them that distinguish them from Arrays. Some of these collections are:

ArrayList
An ArrayList is a dynamic array, the size increases and shrinks when needed and stores elements as generic Object's, this provides us with the ability to store objects with differing types.

Add method
To add objects to the ArrayList use the System.Collections.ArrayList.Add(Object) method The following is an example of adding String objects to an Arraylist.

Queue
A queue is a first-in/first-out style collection. A queue has two unique methods used to enqueue and dequeue information from it. Using the System.Collections.Queue.Enqueue method, an object can be added to the end of the collection. Items can later be removed using the System.Collections.Queue.Dequeue method. This method will take the first object in the queue collection and then remove it from that collection.

Stack
A stack is a last-in/first-out style collection. A stack has three unique methods used to store and retrieve information from it. Using the System.Collections.Stack.Push method, an object can be added to the top of the stack, pushing the objects below it lower into the stack. Items can later be removed from the stack using the System.Collections.Stack.Pop method. This method will return the object at the top of the stack and remove it from the stack. Another method, System.Collections.Stack.Peek, is similar to the Pop method except it returns the object without removing it from the stack.

SortedList
A SortedList collection is a collection of objects that are sorted both by index (similar to an array) and by key. A key works in a manner similar to that of an index; however, unlike an index, a key can be any object.

Generics
Generics provide data structures that work for a variety of data types. While the collections discussed above store items as objects, generic collections store the values in a more specific type such as String or Integer.

List
A List(Of Type) allows a set of values to be accessed by index or as a collection. You can therefore mix array and collection syntax on the same set:

Visual Basic .NET/Collections