
List is a resizable array. It allows you to store a variable number of elements in an expandable collection, similar to an array. The List type in VB.NET offers many useful instance methods. It is one of the most important collections.
Key points: The List type is a dynamic, automatically resizing array. You must specify the type of elements when you declare the List.
First, the Add method on the List type in the VB.NET programming language is used in many programs. The Add method can be used in a loop body to add elements programmatically to the collection. You do not need to predict the final size of the List at all. The internal buffers in the List will dynamically expand. This example demonstrates the Add method.
Program that uses Add on List type [VB.NET]
Module Module1
Sub Main()
Dim list As New List(Of Integer)
list.Add(2)
list.Add(3)
list.Add(5)
list.Add(7)
End Sub
End Module
Let's demonstrate two ways you can loop over the elements in a List type in your VB.NET program. The program text adds three integers to the List contents, and then uses a For Each loop construct, and then finally a For loop construct on that loop. The output of the program demonstrates that the two loops have an equivalent result. Please note how the expression "list.Count - 1" is used for the upper loop bounds on the For loop; this is necessary to keep the index valid throughout.
Program that uses For Each and For loops on List [VB.NET]
Module Module1
Sub Main()
Dim list As New List(Of Integer)
list.Add(2)
list.Add(3)
list.Add(7)
' Loop through list elements.
Dim num As Integer
For Each num In list
Console.WriteLine(num)
Next
' Loop through list with a for to loop.
Dim i As Integer
For i = 0 To list.Count - 1
Console.WriteLine(list.Item(i))
Next i
End Sub
End Module
Output
2
3
7
2
3
7Item access. In the second loop above, you can see that the Item() function is invoked. This is an indexer function and it allows you to access an element at that index inside the List. This style is different than that used in the C# programming language.
Here, we show how you can use the Count property and the Clear() function on the List type instance. The Count property returns the total number of elements in the List, and is similar in semantics to the Length property on an array. The Clear() function removes all the elements from the array so that the next invocation of Count will return zero.
Program that uses Count and Clear [VB.NET]
Module Module1
Sub Main()
' Create a list of booleans.
Dim list As New List(Of Boolean)
list.Add(True)
list.Add(False)
list.Add(True)
' Write the count.
Console.WriteLine(list.Count)
' Clear the list elements.
list.Clear()
' Write the count again.
Console.WriteLine(list.Count)
End Sub
End Module
Output
3
0
Let's look at how you can instantiate the List type and initialize it to have some specific elements, without calling the Add function repeatedly. The benefit to using this syntax is that it reduces the size of your source text, and sometimes increases readability because of that. There is no important difference at the level of the intermediate language of the compiled program.
Program that initializes List type instance [VB.NET]
Module Module1
Sub Main()
' Create a list of three integers.
Dim list As New List(Of Integer)(New Integer() {2, 3, 5})
' Write the count.
Console.WriteLine(list.Count)
End Sub
End Module
Output
3When you have a List type instance, you often need to scan or loop through the elements in the List and use some logical tests on them. For example, perhaps you need to test each element against the value '3' and if that test succeeds, you want to perform some other action. To do this, you can use a For Each loop and then an enclosed If-statement in the VB.NET language. This example demonstrates this usage.
Program that tests elements in List [VB.NET]
Module Module1
Sub Main()
' Create a list of three integers.
Dim list As New List(Of Integer)(New Integer() {2, 3, 5})
' Loop through each number in the list.
' ... Then check it against the integer 3.
Dim num As Integer
For Each num In list
If (num = 3) Then
Console.WriteLine("Contains 3")
End If
Next
End Sub
End Module
Output
Contains 3Here, we note that the String.Join method provides a powerful and efficient way of combining an array of strings into a single string with a specific delimiter character dividing the parts. However, there is no Join method that can effectively be applied directly to the List type, even if the List is of strings. However, you can apply the ToArray parameterless extension method in the VB.NET language, and then pass as an argument this result to the String.Join method, which results in a joined string.
Program that uses String.Join on List [VB.NET]
Module Module1
Sub Main()
' Create a list of strings.
' ... Then use the String.Join method on it.
Dim list As New List(Of String)
list.Add("New York")
list.Add("Mumbai")
list.Add("Berlin")
list.Add("Istanbul")
Console.WriteLine(String.Join(",", list.ToArray))
End Sub
End Module
Output
New York,Mumbai,Berlin,IstanbulOften, you will use the Dictionary type in the VB.NET programming language as well as the List type. In this example, we see how the two types can be used together. We can acquire a List instance of all of the keys from a Dictionary instance. Then, that List can be used just like any other List.
Dictionary ExamplesProgram that uses Keys and List [VB.NET]
Module Module1
Sub Main()
' Create a dictionary of integers and boolean values.
' ... Add two pairs to it.
Dim dictionary As New Dictionary(Of Integer, Boolean)
dictionary.Add(3, True)
dictionary.Add(5, False)
' Get the list of keys.
Dim list As New List(Of Integer)(dictionary.Keys)
' Loop through the list and display the keys.
Dim num As Integer
For Each num In list
Console.WriteLine(num)
Next
End Sub
End Module
Output
3
5The List type in the VB.NET programming language also allows you to insert elements into the List. When you use the Insert instance method on the List type, the first argument must be the desired index for the element; for example, the index 1 will put the element in the second index, because the List is ordered starting at zero.
Program that uses Insert method [VB.NET]
Module Module1
Sub Main()
' Create a list of strings.
Dim list As New List(Of String)
list.Add("spaniel")
list.Add("beagle")
' Insert a pair into the list.
list.Insert(1, "dalmation")
' Loop through the entire list.
Dim str As String
For Each str In list
Console.WriteLine(str)
Next
End Sub
End Module
Output
spaniel
dalmation
beagleIn this example, we look at a specific and less-commonly used method of the List type in the VB.NET language. We show how you can use the GetRange instance method, which receives two arguments: the first being the starting index, and the second being the number of elements (count) you wish to receive.
Program that uses GetRange method [VB.NET]
Module Module1
Sub Main()
' Create a new list of strings and add five strings to it.
Dim list As New List(Of String)(New String() {"nile", _
"amazon", _
"yangtze", _
"mississippi", _
"yellow"})
' Loop through the strings.
Dim str As String
For Each str In list.GetRange(1, 2)
Console.WriteLine(str)
Next
End Sub
End Module
Output
amazon
yangtze
You can also use the Find, FindIndex, FindLast, and FindLastIndex functions on the List type. These provide a way for you to declaratively search your List. This approach can eliminate some complexity from your programs.
List Find Function: FindIndex, FindLast BinarySearch List
In this group of examples, we explored the List type in the VB.NET programming language. The List type is considered a generic, constructed type and because of this it has some performance advantages over other structures. In many programs, the List type is very useful and serves developers well in the long run.
VB.NET Tutorials