Module Module1
Sub Main()
' Input array.
Dim arr() As String = {"cat", "dog", "carrot", "bird"}
' Find element starting with "car".
Dim value1 As String = Array.Find(arr, Function(x) (x.StartsWith("car")))
Console.WriteLine(value1)
' Find element of length 3.
Dim value2 As String = Array.Find(arr, Function(x) (x.Length = 3))
Console.WriteLine(value2)
' Find all elements of length 4 or less.
Dim arr2() As String = Array.FindAll(arr, Function(x) (x.Length <= 4))
Console.WriteLine(String.Join(",", arr2))
End Sub
End Modulecarrot
cat
cat,dog,bird
Notes, lambdas. We use the Function keyword to specify a lambda. The first part is the argument to the Function, and the second is the expression that the Function evaluates and returns.
Summary. Arrays can be searched with loops. This often results in more complex code—we must manage arrays, elements and indexes. With Find and FindAll, we eliminate these forms of complexity.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Mar 10, 2023 (edit).