Learn to program in Visual Basic .NET, the right way

Collections - Which to Use

Which Collection Should I Use?

There are many different types of collections that Visual Basic .NET provides us with, but how do we know which collection to use?

This lesson will be in the form of an FAQ to answer which collection should be used for different scenarios.

I have a set of values and I know how many there are. No more values will be added and none of the existing values will be removed.

Use an array. Arrays are fixed sized collections and are highly efficient if you do not need to add or remove values.

I have a set of values and I need to have the opportunity to add new values or remove existing values, however I need to be able to add/remove items from any index within the collection.

Use a List(Of T). Lists allow you to add/remove items at any index within the collection.

I have a set of values and I need to have the opportunity to add new values or remove existing values, however I want the new values to be added at the end of the collection and the existing values to be removed from the start of the collection.

Use a Queue(Of T). Queues represent a FIFO(first-in-first-out) collection which allow you to add new values to the end of the collection and remove existing values from the start of the collection.

I have a set of values and I need to have the opportunity to add new values or remove existing values, however I want the new values to be added at the end of the collection and the existing values to be removed from the end of the collection.

Use a Stack(Of T). Stacks represent a LIFO(last-in-first-out) collection which allows you to add new values to the end of a collection and remove existing values from the end of the collection.

I have a set of values that are pairs and I need to have the opportunity to add new values or remove existing values, however the one value in the pair needs to be unique.

Use a Dictionary(Of TKey, TValue). Dictionaries represent a collection that stores key/value pairs where the key is a unique value.