Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]
Searches the entire 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)
Syntax
'Declaration
Public Overridable Function BinarySearch ( _
value As Object, _
comparer As IComparer _
) As Integer
public virtual int BinarySearch(
Object value,
IComparer comparer
)
public:
virtual int BinarySearch(
Object^ value,
IComparer^ comparer
)
abstract BinarySearch :
value:Object *
comparer:IComparer -> int
override BinarySearch :
value:Object *
comparer:IComparer -> int
public function BinarySearch(
value : Object,
comparer : IComparer
) : int
Parameters
- value
Type: System. . :: . .Object
The Object to locate. The value can be nullNothingnullptrunita null reference (Nothing in Visual Basic).
- comparer
Type: System.Collections. . :: . .IComparer
The IComparer implementation to use when comparing elements.
-or-
null Nothing nullptr unit a 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.
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. 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 null Nothing nullptr unit 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. 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 null Nothing nullptr unit a null reference (Nothing in Visual Basic) with any type is allowed and does not generate an exception when using IComparable. When sorting, null Nothing nullptr unit a 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.
.NET Framework Security
- Full trust for the immediate caller. This member cannot be used by partially trusted code. For more information, see dd66cd4c-b087-415f-9c3e-94e3a1835f74.