Mono Class Library: System.Array Overview | Members

System.Array.BinarySearch Method

Searches the specified one-dimensional Array for the specified value, using the specified IComparer implementation. [Edit]

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

Parameters

array
A Array to search. [Edit]
value
A object for which to search. [Edit]
comparer
The IComparer implementation to use when comparing elements. Specify a null reference to use the IComparable implementation of each element. [Edit]

Returns

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

Return ValueDescription
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 at least one array element was greater than value.
The bitwise complement of (array.GetLowerBound(0) + array.Length).value was not found, and value was greater than 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 already sorted.

[Edit]

Exceptions

TypeReason
ArgumentException

comparer is null, and both value and at least one element of array do not implement the IComparable interface.

-or-

comparer is null, and 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]
InvalidOperationExceptioncomparer is null, and both 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, comparer).

value is compared to each element of array using comparer until an element with a value greater than or equal to value is found. If comparer is null, the IComparable interface of the element being compared - or of value if the element being compared does not implement the interface - is used. 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