How to search an item in a C# List

Search an Item in a List

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. 


// Create a list of strings
List<string> AuthorList = new List<string>();
AuthorList.Add("Mahesh Chand");
AuthorList.Add("Praveen Kumar");
AuthorList.Add("Raj Kumar");
AuthorList.Add("Nipun Tomar");
AuthorList.Add("Dinesh Beniwal");

int itemPosition = AuthorList.BinarySearch("Raj Kumar");
Console.WriteLine("Item found at position: {0}", itemPosition + 1);

Download Free book: Programming List with C#