TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to VBNET

VB.NET List Find and Exists Examples

Use the Find and Exists Functions with Predicate arguments on the List type.
List, Find. The List Find Function receives a Predicate argument. This argument tests elements to see if they match. Find then returns the element that first matches your Predicate.
Meanwhile, the Exists Functions like the Find Function but it returns true or false if an element exists. With Find and Exists, we replace iterative loops with single Function call.ListFunc, Action, Predicate
Find example. We create a List of 5 Strings—this is the List we will search. We then invoke the Find and FindLast on this List of Strings with different lambda expressions (Predicates).

Find call 1: In the first call to Find, we specify a lambda expression that returns True if the element has length of 5.

Lambda

Find 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
FindIndex. This function works the same way as Find except it returns the index of the match, not the match itself. You should assign an Integer Dim to its result.

Not found: If no match is found, FindIndex returns -1. We must test for this value if the element can possibly not exist.

IndexOf
VB.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
Exists. This Function is just like Find—it accepts a Predicate argument. But it returns a True or False (Boolean) value instead of a List. If a matching element Exists, it returns True.
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
A review. The List Find functions help us search List data. You can search from the beginning with Find and FindIndex, or from the end with FindLast and FindLastIndex.
Lambda expression syntax specifies the predicate function, which tests each element. Exists() and Find() are used with similar syntax. Exists() returns a Boolean, not a List.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf