C# Array BinarySearch() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In C#, Array.BinarySearch looks for a value inside a sorted array. It uses the binary search algorithm, which is fast on large arrays because it jumps to the middle instead of scanning one by one.

If the value exists, you get its index. If it is missing, you get a negative number. Use ~result (bitwise complement) to learn where the value would be inserted to keep the array sorted.

This is a static member of System.Array. You call it with the class name, such as Array.BinarySearch(...). Arrays in C# have a fixed length after creation; use List<T> when the count must grow.

Syntax

Sort first (unless the array is already ordered), then call Array.BinarySearch.

C#
Array.Sort(nums);
int ix = Array.BinarySearch(nums, 30);
CallWhat it does
Array.BinarySearch(nums, 30)Searches the whole sorted array for 30.
Array.BinarySearch(nums, 1, 3, 20)Searches a range: index 1, length 3.

Results are undefined if the array is not sorted according to the comparer you use.

Library declarations
C#
public static int BinarySearch(Array array, object value);
public static int BinarySearch<T>(T[] array, T value);

Parameters

ParameterDescription
arrayRequired. Must be sorted.
valueRequired. The value to find.

Return Value

Return ValueDescription
index (>= 0)The value was found at that index (if duplicates exist, any matching index may be returned).
negativeNot found. ~result is the insertion point.

C# Examples

Example 1 — Find a Value After Sort

Sort numbers, then binary-search for 30.

C#
using System;

class Program {
    static void Main() {
        int[] nums = { 40, 10, 30, 20 };
        Array.Sort(nums);

        int ix = Array.BinarySearch(nums, 30);

        Console.WriteLine("Sorted: " + string.Join(", ", nums));
        Console.WriteLine("Index of 30: " + ix);
    }
}

How It Works

  1. Sort first. Binary search assumes ascending order.
  2. Middle jumps. The algorithm compares against midpoints, not every element in order.
  3. Hit at 2. After sorting, 30 sits at index 2.
  4. Non-negative means found. Check ix >= 0 before using the index.

Example 2 — Negative Result When Missing

Search for a value that is not present and decode the insertion point.

C#
using System;

class Program {
    static void Main() {
        int[] nums = { 10, 20, 40, 50 };
        int ix = Array.BinarySearch(nums, 30);

        Console.WriteLine("Raw result: " + ix);
        if (ix < 0) {
            int insertAt = ~ix;
            Console.WriteLine("Insert 30 at index " + insertAt);
        }
    }
}

How It Works

  1. Negative means missing. Do not use the raw value as an index.
  2. Complement. ~ix turns the failure code into an insertion index.
  3. Keep sorted. Inserting at that index would preserve order.
  4. Array already sorted. This sample starts ordered, so no Sort call.

Example 3 — BinarySearch on Sorted Strings

Order names, then find one alphabetically.

C#
using System;

class Program {
    static void Main() {
        string[] names = { "Dan", "Ann", "Cara", "Bob" };
        Array.Sort(names);

        int ix = Array.BinarySearch(names, "Cara");
        Console.WriteLine(string.Join(", ", names));
        Console.WriteLine("Cara @ " + ix);
    }
}

How It Works

  1. Default comparer. Strings sort in ordinal/culture-sensitive order depending on overload; Sort + BinarySearch pair must match.
  2. After Sort. Alphabetical order places Cara at index 2.
  3. Exact match. BinarySearch looks for equality under the comparer.
  4. Case matters. Default comparisons are case-sensitive for typical string sorts.

Frequently Asked Questions

Yes. Sort the array first (or build it already sorted). Unsorted input gives undefined results.
The value was not found. Use ~result to get the index where it would be inserted.
On large sorted arrays, yes. IndexOf scans from the start and works on unsorted data.
BinarySearch may return any index of a matching value, not necessarily the first.
Yes after sorting the string array. Use matching comparers for Sort and BinarySearch.

Next: Clear()

Set a range of elements to their default values (0 / null / false) with System.Array.

Clear() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful