Search Item In List Using VB.NET

In this article I describe how to search an item in list.
  • 2495

Searching

The BinarySearch method users a binary search algorithm to find an item in the sorted List.
The following code snippet finds an item in a List.

Imports System.Text

Imports System.IO

Imports System.Collections.Generic

Module Module1

    Sub Main()

     ' Create a list of strings

        Dim AuthorList As New List(Of String)()

        AuthorList.Add("Mahesh Chand")

        AuthorList.Add("Praveen Kumar")

        AuthorList.Add("Raj Kumar")

        AuthorList.Add("Nipun Tomar")

        AuthorList.Add("Dinesh Beniwal")

        For Each Author In AuthorList

            Console.WriteLine(Author)

        Next

        Dim itemPosition As Integer = AuthorList.BinarySearch("Raj Kumar")

        Console.WriteLine("Author Raj Kumar found at position: {0}", itemPosition + 1)

        Console.ReadLine()

    End Sub

End Module

Output:

 search-item-from-list.jpg

Categories

More Articles

© 2018 DotNetHeaven. All rights reserved.