Use the BinarySearch() method to search stringArray for 'abc345' : Array Search « Data Structure « C# / CSharp Tutorial






using System;

class MainClass
{

  public static void Main()
  {
    string[] stringArray = {"t", "i", "a", "test", "abc123", "abc345"};
    Array.Sort(stringArray);
        
    int index = Array.BinarySearch(stringArray, "abc345");
    Console.WriteLine("Array.BinarySearch(stringArray, \"abc345\") = " + index);

  }

}
Array.BinarySearch(stringArray, "abc345") = 2








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'