C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Of Integer: The keywords "Of Integer" mean that the List will contain only Integers. Other types, even Objects, are not allowed in it.
Add: The argument to the Add subroutine is the value we want to add to the List. The value is added after all existing elements.
Version 1: We call Add 4 times to add the 4 Integers. We access Count, and get the last element (which is 7) by subtracting 1 from Count.
Version 2: This code does the same thing as version 1, but it uses a shorter syntax form—the List initializer—to create the list.
VB.NET program that uses Add, Count, initializer
Module Module1
Sub Main()
' Version 1: create list of 4 Integers with Add.
Dim list As New List(Of Integer)
list.Add(2)
list.Add(3)
list.Add(5)
list.Add(7)
Console.WriteLine("LIST 1 COUNT: {0}", list.Count)
Console.WriteLine("LIST 1 LAST: {0}", list(list.Count - 1))
' Version 2: create list with initializer.
Dim list2 As New List(Of Integer)({2, 3, 5, 7})
Console.WriteLine("LIST 2 COUNT: {0}", list2.Count)
Console.WriteLine("LIST 2 LAST: {0}", list2(list2.Count - 1))
End Sub
End Module
Output
LIST 1 COUNT: 4
LIST 1 LAST: 7
LIST 2 COUNT: 4
LIST 2 LAST: 7
Part 1: We create a new List of Integers. Then, we add 3 integers to the List contents.
Part 2: We loop over the elements in the List with a For-Each loop construct. On each value, "number" refers to the current element.
For Each, ForPart 3: We use a For-loop. The expression "list.Count - 1" is used for the upper loop bounds. Item() gets each element by its index.
VB.NET program that uses For-Each and For, List
Module Module1
Sub Main()
' Part 1: create List and add 3 Integers.
Dim list As New List(Of Integer)
list.Add(2)
list.Add(3)
list.Add(7)
' Part 2: loop through List elements.
For Each number As Integer In list
Console.WriteLine("FOR EACH: {0}", number)
Next
' Part 3: loop through list with a for-to loop.
For i As Integer = 0 To list.Count - 1
Console.WriteLine("FOR: {0}", list.Item(i))
Next i
End Sub
End Module
Output
FOR EACH: 2
FOR EACH: 3
FOR EACH: 7
FOR: 2
FOR: 3
FOR: 7
Here: We use the Count property and the Clear function on the List type instance.
VB.NET program that uses Count and Clear
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("COUNT: {0}", list.Count)
' Clear the list elements.
list.Clear()
' Write the count again.
Console.WriteLine("COUNT: {0}", list.Count)
End Sub
End Module
Output
COUNT: 3
COUNT: 0
Note: There is no important difference at the level of the intermediate language of the compiled program.
VB.NET program that initializes List instance
Module Module1
Sub Main()
' Create a list of 3 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
3
Tip: To do this, we can use a For-Each loop and then an enclosed If-statement in the program.
If ThenVB.NET program that uses If, List elements
Module Module1
Sub Main()
' Create a list of 3 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.
For Each num As Integer In list
If (num = 3) Then
Console.WriteLine("Contains 3")
End If
Next
End Sub
End Module
Output
Contains 3
Note: The .NET Framework provides a String.Join overload that accepts an argument of type IEnumerable.
JoinHere: We convert a List into an array. We apply the ToArray extension and then pass as an argument this result to String.Join.
ToArrayVB.NET program that uses Join, List
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,Istanbul
Step 1: We create a Dictionary and add 2 keys to it—each key is an Integer, each value is a Boolean.
Step 2: We get the Keys by accessing the Keys property, and then create a new List with the List copy constructor.
Step 3: We use a For-Each loop to enumerate all the Integer keys, writing them to the screen.
VB.NET program that uses Keys and List
Module Module1
Sub Main()
' Step 1: create a dictionary, and add 2 pairs.
Dim dictionary As New Dictionary(Of Integer, Boolean)
dictionary.Add(3, True)
dictionary.Add(5, False)
' Step 2: get the list of keys.
Dim list As New List(Of Integer)(dictionary.Keys)
' Step 3: loop and display the keys.
For Each number As Integer In list
Console.WriteLine("KEY: {0}", number)
Next
End Sub
End Module
Output
KEY: 3
KEY: 5
Argument 1: The first argument must be the desired index for the element. The index 1 will make the element the second element.
Argument 2: The second argument is the element to insert. Here we insert a dog breed name into our List.
VB.NET program that uses Insert method
Module Module1
Sub Main()
' Create a list of strings.
Dim list As New List(Of String)
list.Add("spaniel")
list.Add("beagle")
' Insert a string into the list.
list.Insert(1, "dalmatian")
' Loop through the entire list.
For Each str As String In list
Console.WriteLine(str)
Next
End Sub
End Module
Output
spaniel
dalmatian
beagle
Tip: The type of the array we insert (like Integer or String) must match the target List's element type.
ArrayTip 2: We can insert one List into another with AddRange and InsertRange—the syntax is the same (just pass a list instead of an array).
VB.NET program that uses AddRange, InsertRange
Module Module1
Sub Main()
' Initial list.
Dim list As List(Of Integer) = New List(Of Integer)({1, 2})
' Add ints on the end.
list.AddRange(New Integer() {3, 4})
' Insert ints at the start.
list.InsertRange(0, New Integer() {-2, -1})
' Display final list.
For Each value In list
Console.WriteLine("FINAL LIST: {0}", value)
Next
End Sub
End Module
Output
FINAL LIST: -2
FINAL LIST: -1
FINAL LIST: 1
FINAL LIST: 2
FINAL LIST: 3
FINAL LIST: 4
Step 1: We create a List of several strings. We specify string literals containing the names of rivers.
Step 2: We call GetRange in a For-Each loop—the loop only calls GetRange once, and it returns river names starting at the specified index.
VB.NET program that uses GetRange method
Module Module1
Sub Main()
' Step 1: create a List.
Dim list As New List(Of String)(
New String() {"nile", "amazon", "yangtze", "mississippi", "yellow"})
' Step 2: call GetRange.
' ... Loop through the strings returned by GetRange.
For Each value As String In list.GetRange(1, 2)
Console.WriteLine("RIVER: {0}", value)
Next
End Sub
End Module
Output
RIVER: amazon
RIVER: yangtze
Note: If we call Remove() with a value that is not found in the List, nothing happens.
VB.NET program that uses Remove
Module Module1
Sub Main()
Dim numbers = New List(Of Integer)({10, 20, 30})
' Remove this element by value.
numbers.Remove(20)
For Each number In numbers
Console.WriteLine("NOT REMOVED: {0}", number)
Next
' This will not change anything.
numbers.Remove(3000)
End Sub
End Module
Output
NOT REMOVED: 10
NOT REMOVED: 30
Part A: Here we call IndexOf and the value is located in the list. The value 20 occurs at index 1, so 1 is returned.
Part B: We try to find 100, but it does not exist. IndexOf returns -1 if no matching value exists in the List.
VB.NET program that uses IndexOf
Module Module1
Sub Main()
Dim sizes As List(Of Integer) = New List(Of Integer)
sizes.Add(10)
sizes.Add(20)
sizes.Add(30)
' Part A: the value 20 occurs at the index 1.
Dim index20 As Integer = sizes.IndexOf(20)
Console.WriteLine(index20)
' Part B: the value 100 does not occur, so IndexOf returns -1.
Dim index100 As Integer = sizes.IndexOf(100)
If index100 = -1 Then
Console.WriteLine("Not found")
End If
End Sub
End Module
Output
1
Not found
Here: We create a List and an array in Main. We pass those of those to the PrintElements Sub—it handles all IEnumerable types.
Tip: Often we can use IEnumerable-receiving functions to share code that must act upon Lists and arrays.
IEnumerableVB.NET program that uses List with IEnumerable Interface
Module Module1
Sub Main()
' Create List and pass it as an IEnumerable.
Dim list As List(Of Integer) = New List(Of Integer)({10, 20, 30})
PrintElements(list)
' Use array as an IEnumerable.
Dim array As Integer() = {100, 200, 300}
PrintElements(array)
End Sub
Sub PrintElements(ByVal elements As IEnumerable(Of Integer))
' Handle elements in IEnumerable.
For Each element In elements
Console.WriteLine("Element: {0}", element)
Next
End Sub
End Module
Output
Element: 10
Element: 20
Element: 30
Element: 100
Element: 200
Element: 300
Tip: Knowing the difference between List and Dictionary is important skill for VB.NET developers—this will help many real-world programs.