This is a great proof that object oriented programming is good for the evolution, imagine an array like we’ve seen in the last tutorial, but controlled in a way to make it more efficient and to abstract all the plumbing that is required to expand the array and so on. An easy to use solution!
The Collection.Generics have been invented to simplify our lives so we don’t have to implement these complex data structures every time we want to use them.
The most important ones, in my opinion, are the List, the Dictionary, the Queue and the Stack.
They will be divided into one tutorial per collection since I want to give examples and go in more details about each one. The first one we’re going to go through is the List. After all the Generics have been covered, I will give you a crash course on Linq (maybe after we’ve seen how objects and classes work), we will then finish this topic.
The List offers more methods and more performance than its younger brother, the ArrayList. You should use a List for general purpose collection operations like storing a list of students for example!
First, let’s create a new instance of the List, meaning that we’re going to use the List class or template if you prefer, to create our own real List object.
Module Program Sub Main() 'Create (declare and initialize) a new instance of the Generic class List Dim ListOfNames = New List(Of String) End Sub End Module
This will create a real (because of the New) List object that can contain only String types elements. We are now ready to add data!
It’s easy to add data to your list, you have to use the method Add(T Element). Why am I using T as the variable type? It’s because you can define your own type of objects that will fit into your list, here we are using Of String but you could use pretty much anything! Here is what it looks like in code.
Module Program Sub Main() 'Create a new instance of the Generic class List Dim ListOfNames = New List(Of String) 'Add 3 elements to the list ListOfNames.Add("Alexandre") ListOfNames.Add("Maxime") ListOfNames.Add("Daniel") End Sub End Module
You can also use AddRange(Collection) to add another collection’s data to this one, very practical because you don’t have to loop through all the elements and add them one by one.
Deleting an element from your list is fairly similar, you have to use one of the methods: Remove(T Element), RemoveAt(Integer Index), RemoveAll(Predicate function) or RemoveRange(Integer StartingIndex, Integer Count).
The Remove method lets you remove a specific element, here is an example.
Remark: if the element you wish to remove does not exist, it will not remove anything and will not prompt an error.
Another option is use the RemoveAt(Integer Index) method that will remove an element at a specific Index, for example, “Alexandre” is at index 0, I can remove this element like that:
We won’t look at the RemoveAll method, we are going to keep that one for later, we need Linq!
As for the RemoveRange method, you can remove from one element counting. For example, if the List contains 5 elements, I can remove the element from 3 to 5 by using this method. Remember that the indexes start at 0!
The removal starts at 3 and removes 3 elements from the List.
Insert(Integer Position, T Element)
You can insert an element at a specific position in your List with Insert(Integer Position, T Element) method that can be used as follow.
In this example, we insert the String element “Alexandre” at the position 1 in the List.
This method ensures that an element is present in the List, if it is, it will return the Boolean type value True, if it isn’t, it’s going to return False. That’s why you can easily use it in a If statement!
If ListOfNames.Contains("Alexandre") Then Console.WriteLine("The list contains the name Alexandre!") End If
The Contains method takes a T Element as parameter, be careful if the element is an object of another type, you may need to implement the IEquatable interface, this is an advanced topic that we will cover later. IF you wish to have more information on that subject, you can refer to the MSDN Website.
The Clear() method can be used to clear the content of a Collection completely.
Again, I strongly suggest that you add breakpoints in this code to better understand how it works and be able to add operations on the List. For example, you can try to do the same thing with a List(Of Integer) or even add numbers with a loop.
Notice that you can click on the small arrow to expand results after you hover on a variable (in this case, the List itself).
Here is the full code I wrote as a reference.
Module Program Sub Main() 'Create a new instance of the Generic class List Dim ListOfNames = New List(Of String) 'Add elements to the List ListOfNames.Add("Alexandre") ListOfNames.Add("Maxime") ListOfNames.Add("Daniel") 'Remove elements from the List ListOfNames.Remove("Maxime") ListOfNames.RemoveAt(0) 'Insert back Alexandre in the List ListOfNames.Insert(1, "Alexandre") 'Check if the List contains the element "Alexandre" that we just inserted If ListOfNames.Contains("Alexandre") Then Console.WriteLine("The list contains the name Alexandre!") End If Console.ReadKey() End Sub End Module
These are the basic methods you can use on a list, you will see similarities between other Generic Collections methods. There are more methods that we will need to explore later like Sort(), All(), Select(), Where(), Any() and so on when we will have a closer look at Linq.
If you need more information on the Lists, please refer to the MSDN Website on the topic or you can always post your questions in the comments.