Use the BinarySearch() method to search intArray for the number 4: BinarySearch() returns a negative value : Array Search « Data Structure « C# / CSharp Tutorial






using System;

class MainClass
{

  public static void Main()
  {
    int[] intArray = {5, 2, 3, 1, 6, 9, 7, 14, 25};
    Array.Sort(intArray);
    
    int index = Array.BinarySearch(intArray, 4);
    Console.WriteLine("Array.BinarySearch(intArray, 4) = " + index);
  }

}
Array.BinarySearch(intArray, 4) = -4








11.17.Array Search
11.17.1.Use the BinarySearch() method to search intArray for the number 5
11.17.2.Use the BinarySearch() method to search intArray for the number 4: BinarySearch() returns a negative value
11.17.3.Use the BinarySearch() method to search stringArray for 'abc345'
11.17.4.Use the BinarySearch() method to search charArray for 'o'