Example of BinarySearch Method in Generic List C#

using System; 
using System.Collections.Generic; 
 
namespace w3listexample { 
    class w3listexample { 
 
		static void Main(string[] args) { 
		 List<string> list = new List<string>() { "A", "B", "C", "D", "E", "F", "G"}; 
 
 		int index1 = list.BinarySearch("F"); 
		Console.WriteLine("Index 1: {0}", index1) ; 
 
		int index2 = list.BinarySearch("Z"); 
		Console.WriteLine("Index 2 : {0} {1}", index2, ~index2) ; 
 
		// wait for input before exiting 
		Console.WriteLine("Press enter to finish"); 
		Console.ReadLine(); 
		} 
        } 
}

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.