Array.BinarySearch Method in C#


Get the location of array elements using the BinarySearch method.

Set a string array −

string[] str = { "a", "m", "i", "t"};

Now get the location of character ‘t’ using Array.BinarySearch −

Array.BinarySearch(str, "t");

Here is the complete code −

Example

 Live Demo

using System;
using System.Text;
public class Demo {
   public static void Main() {
      string[] str = { "a", "m", "i", "t"};
      // Using BinarySearch method to get location of character 't'
      int res = Array.BinarySearch(str, "t");
      // displaying the location
      Console.WriteLine("Index : "+res);
   }
}

Output

Index : 3

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 22-Jun-2020

56 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements