Searches a section of a one-dimensional Array for a value, using the IComparable interface implemented by each element of the Array and by the specified value.
[Visual Basic] Overloads Public Shared Function BinarySearch( _ ByVal array As Array, _ ByVal index As Integer, _ ByVal length As Integer, _ ByVal value As Object _ ) As Integer [C#] public static int BinarySearch( Array array, int index, int length, object value ); [C++] public: static int BinarySearch( Array* array, int index, int length, Object* value ); [JScript] public static function BinarySearch( array : Array, index : int, length : int, 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. |
ArgumentOutOfRangeException | index is less than the lower bound of array.
-or- length is less than zero. |
ArgumentException | index and length do not denote a valid range in array.
-or- 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