Mono Class Library: System.Collections.ArrayList Overview | Members

System.Collections.ArrayList.BinarySearch Method

Searches the current instance for the specified object.

public virtual int BinarySearch (object value)

Parameters

value
The object for which to search.

Returns

A int that specifies the results of the search as follows:

Return ValueDescription
The index of value in the current instance.value was found.
The bitwise complement of the index of the first element that is greater than value.value was not found, and at least one element in the current instance is greater than value.
The bitwise complement of the ArrayList.Count of the current instance.value was not found, and value is greater than all elements in the current instance.

Note: If value is not found and the current instance is already sorted, the bitwise complement of the return value indicates the index in the current instance where value would be found.

Exceptions

TypeReason
ArgumentExceptionvalue is not assignment-compatible with at least one element in the current instance.
InvalidOperationException Both value and at least one element involved in the search of the current instance do not implement the IComparable interface.

Remarks

This method performs a binary search.

Note: A null reference can be compared with any type; therefore, comparisons with a null reference do not generate exceptions. A null reference evaluates to less than any non-null object, or equal to another null reference, when the two are compared.

Operation
As described above.

This method uses Array.BinarySearch(Array, object) to search for value.

value is compared to elements in a binary search of the current instance until an element with a value greater than or equal to value is found. The IComparable implementation of the element being compared -- or of value if the element being compared does not implement the interface -- is used to make the comparison. If value does not implement the IComparable interface and is compared to an element that does not implement the interface, InvalidOperationException is thrown. If the current instance is not already sorted, correct results are not guaranteed.

Note: For the default implementation, this method is an O(log2n) operation where n is equal to the ArrayList.Count of the current instance.

Requirements

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