Searches a one-dimensional Array for a specific element, using the IComparable interface implemented by each element of the Array and by the specified Object.
[Visual Basic] Overloads Public Shared Function BinarySearch( _ ByVal array As Array, _ ByVal value As Object _ ) As Integer [C#] public static int BinarySearch( Array array, object value ); [C++] public: static int BinarySearch( Array* array, Object* value ); [JScript] public static function BinarySearch( array : Array, value : Object ) : int;
The index of the value in the Array, if value is found. Otherwise, a negative number, which is the bitwise complement of the index of the first element that is larger than value.
Exception Type | Condition |
---|---|
ArgumentNullException | array is a null reference (in Visual Basic Nothing). |
RankException | array is multidimensional. |
ArgumentException | Neither value nor the elements of array implement the IComparable interface.
-or- value is not of the same type as the elements of array. |
value and each element of array must implement the IComparable interface, which is used for comparisons.
If array is not already sorted according to the IComparable interface, the result may be incorrect.
a null reference (Nothing) can always be compared with any other type; therefore, comparisons with a null reference (Nothing) will not generate an exception. When sorting, a null reference (Nothing) is considered to be less than any other object.
The bitwise complement operator (~) can be applied to a negative result to produce the index of the first element (if any) that is larger than the specified search value.
Array Class | Array Members | System Namespace | Array.BinarySearch Overload List | IComparable | Sort