NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

Array.BinarySearch (Array, Int32, Int32, Object, IComparer)

Searches a section of a one-dimensional Array for a value, using the specified IComparer interface.

[Visual Basic]
Overloads Public Shared Function BinarySearch( _
   ByVal array As Array, _
   ByVal index As Integer, _
   ByVal length As Integer, _
   ByVal value As Object, _
   ByVal comparer As IComparer _
) As Integer
[C#]
public static int BinarySearch(
   Array array,
   int index,
   int length,
   object value,
   IComparer comparer
);
[C++]
public: static int BinarySearch(
   Array* array,
   int index,
   int length,
   Object* value,
   IComparer* comparer
);
[JScript]
public static function BinarySearch(
   array : Array,
   index : int,
   length : int,
   value : Object,
   comparer : IComparer
) : int;

Parameters

array
The one-dimensional Array to search.
index
The starting index of the range to search.
length
The length of the range to search.
value
The Object to search for.
comparer
The IComparer implementation to use when comparing elements.

-or-

a null reference (in Visual Basic Nothing) to use the IComparable implementation of each element.

Return Value

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.

Exceptions

Exception Type Condition
ArgumentNullException array is a null reference (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-

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.

Remarks

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 using IComparable. 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.

See Also

Array Class | Array Members | System Namespace | Array.BinarySearch Overload List | IComparer | IComparable | Sort