C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Find call 1: In the first call to Find, we specify a lambda expression that returns True if the element has length of 5.
LambdaFind call 2: Here we also use a lambda expression Predicate. This lambda returns True if the first character of the String is uppercase.
FindLast: This Function works the same way as Find except it searches starting from the final element.
VB.NET program that uses Find and FindLast
Module Module1
Sub Main()
Dim list As List(Of String) = New List(Of String)({"dot", "net", "Codex", "Thank", "You"})
' Find first 5-letter string.
Dim val As String = list.Find(Function(value As String)
Return value.Length = 5
End Function)
Console.WriteLine("FIND: {0}", val)
' Find first uppercased string.
val = list.Find(Function(value As String)
Return Char.IsUpper(value(0))
End Function)
Console.WriteLine("FIND: {0}", val)
' Find last 5-letter string.
val = list.FindLast(Function(value As String)
Return value.Length = 5
End Function)
Console.WriteLine("FINDLAST: {0}", val)
End Sub
End Module
Output
FIND: Codex
FIND: Thank
FINDLAST: Thank
Not found: If no match is found, FindIndex returns -1. We must test for this value if the element can possibly not exist.
IndexOfVB.NET program that uses FindIndex
Module Module1
Sub Main()
Dim list As List(Of String) = New List(Of String)({"bird", "cat", "car"})
' Find index of first string starting with "c."
Dim index As Integer = List.FindIndex(Function(value As String)
Return value(0) = "c"c
End Function)
Console.WriteLine("FIRST C: {0}", index)
End Sub
End Module
Output
FIRST C: 1
VB.NET program that uses List Exists
Module Module1
Sub Main()
Dim list As List(Of Integer) = New List(Of Integer)({20, -1, 3, 4})
' See if a negative number exists.
Dim result = list.Exists(Function(value As Integer)
Return value < 0
End Function)
Console.WriteLine("EXISTS: {0}", result)
' See if a huge number exists.
result = list.Exists(Function(value As Integer)
Return value >= 1000
End Function)
Console.WriteLine("EXISTS: {0}", result)
End Sub
End Module
Output
EXISTS: True
EXISTS: False