BinarySearch Method (Int32, Int32, Object, ICom...
.NET Framework Class Library
ArrayList..::.BinarySearch Method (Int32, Int32, Object, IComparer)

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

Namespace:  System.Collections
Assembly:  mscorlib (in mscorlib.dll)
Visual Basic (Declaration)
Public Overridable Function BinarySearch ( _
    index As Integer, _
    count As Integer, _
    value As Object, _
    comparer As IComparer _
) As Integer
Visual Basic (Usage)
Dim instance As ArrayList
Dim index As Integer
Dim count As Integer
Dim value As Object
Dim comparer As IComparer
Dim returnValue As Integer

returnValue = instance.BinarySearch(index, _
    count, value, comparer)
C#
public virtual int BinarySearch(
    int index,
    int count,
    Object value,
    IComparer comparer
)
Visual 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
Type: System..::.Int32
The zero-based starting index of the range to search.
count
Type: System..::.Int32
The length of the range to search.
value
Type: System..::.Object
The Object to locate. The value can be nullNothingnullptra null reference (Nothing in Visual Basic).
comparer
Type: System.Collections..::.IComparer
The IComparer implementation to use when comparing elements.
-or-
nullNothingnullptra null reference (Nothing in Visual Basic) to use the default comparer that is the IComparable implementation of each element.

Return Value

Type: System..::.Int32
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.
ExceptionCondition
ArgumentException

index and count do not denote a valid range in the ArrayList.

-or-

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

InvalidOperationException

comparer is nullNothingnullptra 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.

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. The elements of the ArrayList must already be sorted in increasing value according to the sort order defined by comparer; otherwise, the result might be incorrect.

If comparer is nullNothingnullptra null reference (Nothing in Visual Basic), the comparison is done using the IComparable implementation provided by the element itself or by the specified value. The elements of the ArrayList must already be sorted in increasing value according to the sort order defined by the IComparable implementation; otherwise, the result might be incorrect.

Comparing nullNothingnullptra null reference (Nothing in Visual Basic) with any type is allowed and does not generate an exception when using IComparable. When sorting, nullNothingnullptra null reference (Nothing in Visual Basic) 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(log n) operation, where n is count.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
Page view tracker