asp.net.ph

Array.BinarySearch Method ( Array, Object, IComparer )

System Namespace   Array Class


Searches an entire one-dimensional sorted Array for a value, using the specified IComparer interface.

[ VB ]
<Serializable>
Overloads Public Shared Function BinarySearch ( _
   ByVal array As Array, _
   ByVal value As Object, _
   ByVal comparer As IComparer _
) As Integer

[ C# ]
[Serializable]
public static int BinarySearch (
   Array array,
   object value,
   IComparer comparer
);

[ C++ ]
[Serializable]
public: static int BinarySearch (
   Array* array,
   Object* value,
   IComparer* comparer
);

[JScript ]
public Serializable
static function BinarySearch (
   array : Array,
   value : Object,
   comparer : IComparer
) : int;

Parameters

array
The one-dimensional Array to search.
value
The object to search for.
comparer
The IComparer implementation to use when comparing elements.

-or-

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

Return Value

The index of the specified value in the specified array, if value is found.

-or-

A negative number, which is the bitwise complement of the index of the first element that is larger than value, if value is not found and value is less than one or more elements in array.

-or-

A negative number, which is the bitwise complement of ( the index of the last element + 1 ) , if value is not found and value is greater than any of the elements in array.

Exceptions


Exception Type Condition
ArgumentNullException array is a null reference ( Nothing in Visual Basic ).
RankException array is multidimensional.
ArgumentException comparer is a null reference ( Nothing in Visual Basic ) , and neither value nor the elements of array implement the IComparable interface.

-or-

comparer is a null reference ( Nothing ) , and value is of a type that is not compatible with the elements of array.

InvalidOperationException The comparer throws an exception.

Remarks

The comparer customizes how the elements are compared. For example, you can use a System.Collections.CaseInsensitiveComparer as the comparer to perform case-insensitive string searches.

If comparer is not a null reference ( Nothing in Visual Basic ) , the elements of array are compared to the specified value using the specified IComparer implementation. If the elements of array are not already sorted in increasing value according to comparer, the result might be incorrect.

If comparer is a null reference ( Nothing ) , the comparison is done using the IComparable implementation provided by the element itself or by the specified value. If the elements of array are not already sorted in increasing value according to the IComparable implementation, the result might be incorrect.

Duplicate elements are allowed. If the Array contains more than one element with the same value, the method returns only one of the occurrences, and it might return any one of the occurrences, not necessarily the first one.

A null reference ( Nothing ) can always be compared with any other type; therefore, comparisons with a null reference ( Nothing ) do not generate an exception. When sorting, a null reference ( Nothing ) is considered to be less than any other object.

If the Array does not contain the specified value, the method returns a negative integer. You can apply the bitwise complement operator ( ~ ) 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 Members   Array.BinarySearch Overload List   IComparer   IComparable   Sort Skip Navigation Links




Home
Suggested Reading


Previous page Back to top Next page

© 2000-2010 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support asp.net.ph