This documentation is archived and is not being maintained.

Array.BinarySearch Method (Array, Int32, Int32, Object)

.NET Framework 1.1

Searches a section of a one-dimensional sorted Array for a value, using the IComparable interface implemented by each element of the Array and by the specified value.

[Visual Basic]
Overloads Public Shared Function BinarySearch( _
   ByVal array As Array, _
   ByVal index As Integer, _
   ByVal length As Integer, _
   ByVal value As Object _
) As Integer
public static int BinarySearch(
 Array array,
 int index,
 int length,
 object value
public: static int BinarySearch(
 Array* array,
 int index,
 int length,
 Object* value
public static function BinarySearch(
   array : Array,
 index : int,
 length : int,
 value : Object
) : int;


The one-dimensional Array to search.
The starting index of the range to search.
The length of the range to search.
The object to search for.

Return Value

The index of the specified value in the specified array, if value is found.


A negative number, which is the bitwise complement of the index of the first element that is larger than value, if value is not found and value is less than one or more elements in array.


A negative number, which is the bitwise complement of (the index of the last element + 1), if value is not found and value is greater than any of the elements in array.


Exception Type Condition
ArgumentNullException array is a null reference (Nothing in Visual Basic).
RankException array is multidimensional.
ArgumentOutOfRangeException index is less than the lower bound of array.


length is less than zero.

ArgumentException index and length do not specify a valid range in array.


value is of a type that is not compatible with the elements of array.

InvalidOperationException Neither value nor the elements of array implement the IComparable interface.


Either value or every element of array must implement the IComparable interface, which is used for comparisons. If the elements of array are not already sorted in increasing value according to the IComparable implementation, the result might be incorrect.

Duplicate elements are allowed. If the Array contains more than one element equal to value, the method returns the index of only one of the occurrences, and not necessarily the first one.

A null reference (Nothing in Visual Basic) can always be compared with any other type; therefore, comparisons with a null reference (Nothing) do not generate an exception. When sorting, a null reference (Nothing) is considered to be less than any other object.

If the Array does not contain the specified value, the method returns a negative integer. You can apply the bitwise complement operator (~) to a negative result to produce the index of the first element, if any, that is larger than the specified search value.


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, Common Language Infrastructure (CLI) Standard

See Also

Array Class | Array Members | System Namespace | Array.BinarySearch Overload List | IComparable | Sort | Performing Culture-Insensitive String Operations in Arrays