Searches a one-dimensional Array for a value, using the specified IComparer interface.
[Visual Basic] Overloads Public Shared Function BinarySearch( _ ByVal array As Array, _ ByVal value As Object, _ ByVal comparer As IComparer _ ) As Integer [C#] public static int BinarySearch( Array array, object value, IComparer comparer ); [C++] public: static int BinarySearch( Array* array, Object* value, IComparer* comparer ); [JScript] public static function BinarySearch( array : Array, value : Object, comparer : IComparer ) : int;
-or-
a null reference (in Visual Basic Nothing) to use the IComparable implementation of each element.
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 (Nothing). |
RankException | array is multidimensional. |
ArgumentException | comparer is a null reference (Nothing), and neither value nor the elements of array implement the IComparable interface.
-or- comparer is a null reference (Nothing), and value is not of the same type as the elements of array. |
If comparer is specified and array is not already sorted according to the sort order defined by comparer, the result may be incorrect.
If comparer is not specified, 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 | IComparer | IComparable | Sort