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.
Assembly: mscorlib (in mscorlib.dll)
public virtual int BinarySearch( int index, int count, Object value, IComparer comparer )
- Type: System.Int32
The zero-based starting index of the range to search.
- Type: System.Int32
The length of the range to search.
index and count do not denote a valid range in the ArrayList.
comparer is null and value is not of the same type as the elements of the ArrayList.
index is less than zero.
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 null, 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 with any type is allowed and does not generate an exception when using IComparable. When sorting, null 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 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.