using System; using System.Collections; class Program { static void Main(string[] args) { ArrayList arrList = new ArrayList(); arrList.Add("apple"); arrList.Add("banana"); arrList.Add("cherry"); arrList.Add("kiwi"); arrList.Add("orange"); int index = arrList.BinarySearch("cherry"); Console.WriteLine(index); // returns 2 index = arrList.BinarySearch("pear"); Console.WriteLine(index); // returns -5 } }
using System; using System.Collections; class Program { static void Main(string[] args) { ArrayList arrList = new ArrayList(); arrList.Add(30); arrList.Add(10); arrList.Add(50); arrList.Add(20); arrList.Add(40); arrList.Sort(); int index = arrList.BinarySearch(40); Console.WriteLine(index); // returns 3 } }The package library used in both examples is System.Collections.