Mono Class Library: System.Array Overview | Members

System.Array.BinarySearch Method

Searches the specified one-dimensional Array for the specified object. [Edit]

[System.Runtime.ConstrainedExecution.ReliabilityContract(System.Runtime.ConstrainedExecution.Consistency.WillNotCorruptState, System.Runtime.ConstrainedExecution.Cer.MayFail)]
public static int BinarySearch (Array array, object value)

Parameters

array
A Array to search for an object. [Edit]
value
A object for which to search. [Edit]

Returns

A int with one of the following values based on the result of the search operation.

Return Value Description
The index of value in the array.value was found.
The bitwise complement of the index of the first element that is larger than value.value was not found and the value of at least one element of array was greater than value.
The bitwise complement of (array.GetLowerBound(0) + array.Length).value was not found, and value was greater than the value of all array elements.

Note: If value is not found, the caller can take the bitwise complement of the return value to determine the index where value would be found in array if it is sorted already.

[Edit]

Exceptions

TypeReason
ArgumentException

Both value and at least one element of array do not implement the IComparable interface.

-or-

value is not assignment-compatible with at least one element of array.

-or-

array.UpperBound == int.MaxValue.

[Edit]
ArgumentNullExceptionarray is null. [Edit]
RankExceptionarray has more than one dimension. [Edit]
InvalidOperationExceptionBoth value and at least one element of array do not implement the IComparable interface. [Edit]

Remarks

This version of Array.BinarySearch(Array, object) is equivalent to Array.BinarySearch(Array, object)(array, array.GetLowerBound(0), array.Length, value, null).

value is compared to each element of array using the IComparable interface of the element being compared - or of value if the element being compared does not implement the interface - until an element with a value greater than or equal to value is found. If value does not implement the IComparable interface and is compared to an element that does not implement the IComparable interface, a InvalidOperationException exception is thrown. If array is not already sorted, correct results are not guaranteed.

Note: A null reference can be compared with any type; therefore, comparisons with a null reference do not generate exceptions.

[Edit]

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0