C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
IndexOf: We then call the Array.IndexOf Function. It returns the value 2, meaning it found the value 5 at index 2.
LastIndexOf: We finally call LastIndexOf. It returns the value 5, meaning it found the value 5 at index 5.
Here: We see the difference between IndexOf and LastIndexOf. They locate different elements in some arrays.
VB.NET program that uses Array.IndexOf, LastIndexOf
Module Module1
Sub Main()
Dim arr(5) As Integer
arr(0) = 1
arr(1) = 3
arr(2) = 5
arr(3) = 7
arr(4) = 8
arr(5) = 5
' Search array with IndexOf.
Dim index1 As Integer = Array.IndexOf(arr, 5)
Console.WriteLine(index1)
' Search with LastIndexOf.
Dim index2 As Integer = Array.LastIndexOf(arr, 5)
Console.WriteLine(index2)
End Sub
End Module
Output
2
5
VB.NET program that uses IndexOf, finds no match
Module Module1
Sub Main()
' An array.
Dim arr() As Integer = {10, 20, 30}
' Search for a value that does not exist.
Dim result As Integer = Array.IndexOf(arr, 100)
Console.WriteLine(result)
End Sub
End Module
Output
-1