Export (0) Print
Expand All

ArrayList.BinarySearch Method (Int32, Int32, Object, IComparer)

.NET Framework 1.1

Searches a section of the sorted ArrayList for an element using the specified comparer and returns the zero-based index of the element.

[Visual Basic]
Overloads Public Overridable Function BinarySearch( _
   ByVal index As Integer, _
   ByVal count As Integer, _
   ByVal value As Object, _
   ByVal comparer As IComparer _
) As Integer
[C#]
public virtual int BinarySearch(
 int index,
 int count,
 object value,
 IComparer comparer
);
[C++]
public: virtual int BinarySearch(
 int index,
 int count,
 Object* value,
 IComparer* comparer
);
[JScript]
public function BinarySearch(
   index : int,
 count : int,
 value : Object,
 comparer : IComparer
) : int;

Parameters

index
The zero-based starting index of the range to search.
count
The length of the range to search.
value
The Object to locate. The value can be a null reference (Nothing in Visual Basic).
comparer
The IComparer implementation to use when comparing elements.

-or-

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

Return Value

The zero-based index of value in the sorted ArrayList, if value is found; otherwise, a negative number, which is the bitwise complement of the index of the next element that is larger than value or, if there is no larger element, the bitwise complement of Count.

Exceptions

Exception Type Condition
ArgumentException index and count do not denote a valid range in the ArrayList.

-or-

comparer is a null reference (Nothing in Visual Basic) and neither value nor the elements of ArrayList implement the IComparable interface.

InvalidOperationException comparer is a null reference (Nothing in Visual Basic) and value is not of the same type as the elements of the ArrayList.
ArgumentOutOfRangeException index is less than zero.

-or-

count is less than zero.

Remarks

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

If comparer is provided, the elements of the ArrayList are compared to the specified value using the specified IComparer implementation. If the ArrayList is not already sorted according to the sort order defined by comparer, the result might be incorrect.

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

Comparing a null reference (Nothing) with any type is allowed and does not generate an exception when using IComparable. When sorting, a null reference (Nothing) is considered to be less than any other object.

If the ArrayList 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.

If the ArrayList does not contain the specified value, the method returns a negative integer. You can apply the bitwise complement operation (~) to this negative integer to get the index of the first element that is larger than the search value. When inserting the value into the ArrayList, this index should be used as the insertion point to maintain the sort order.

This method is an O(log2 n) operation, where n is the number of elements in the range.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

ArrayList Class | ArrayList Members | System.Collections Namespace | ArrayList.BinarySearch Overload List | IComparer | IComparable | Performing Culture-Insensitive String Operations in Collections

Show:
© 2014 Microsoft