Mono Class Library: System.Array Overview | Members

System.Array.BinarySearch Method

Searches the specified section of the specified one-dimensional Array for the specified value. [Edit]

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

Parameters

array
A Array to search. [Edit]
index
A int that contains the index at which searching starts. [Edit]
length
A int that contains the number of elements to search, beginning with index . [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 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 in the range of index to index + length - 1 was greater than value.
The bitwise complement of (index + length).value was not found, and value was greater than all array elements in the range of index to index + length- 1.

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

[Edit]

Exceptions

TypeReason
ArgumentNullExceptionarray is null . [Edit]
RankExceptionarray has more than one dimension. [Edit]
ArgumentOutOfRangeException

index < array.GetLowerBound(0).

-or-

length < 0.

[Edit]
ArgumentException

index and length do not specify a valid range in array (i.e. index + length > array.GetLowerBound(0) + array.Length).

-or-

array.UpperBound == int.MaxValue.

[Edit]
InvalidOperationExceptionEither value or at least one element of array does 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