Skip to content

M_CodeJam_Collections_ArrayExtensions_BinarySearch__1

Andrew Koryavchenko edited this page Jun 17, 2018 · 7 revisions

ArrayExtensions.BinarySearch(T) Method (T[], Int32, Int32, T)

Searches a range of elements in a one-dimensional sorted Array for a value, using the IComparable(T) generic interface implemented by each element of the Array and by the specified value.

Namespace: CodeJam.Collections
Assembly: CodeJam (in CodeJam.dll) Version: 2.1.0.0

Syntax

C#

public static int BinarySearch<T>(
	this T[] array,
	int index,
	int length,
	T value
)

VB

<ExtensionAttribute>
Public Shared Function BinarySearch(Of T) ( 
	array As T(),
	index As Integer,
	length As Integer,
	value As T
) As Integer

F#

[<ExtensionAttribute>]
static member BinarySearch : 
        array : 'T[] * 
        index : int * 
        length : int * 
        value : 'T -> int 

Parameters

 

array
Type: T[]
The sorted one-dimensional, zero-based Array to search.
index
Type: System.Int32
The starting index of the range to search.
length
Type: System.Int32
The length of the range to search.
value
Type: T
The object to search for.

Type Parameters

 

T
The type of the elements of the array.

Return Value

Type: Int32
The index of the specified value in the specified array, if value is found. If value is not found and value is less than one or more elements in array, 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 greater than any of the elements in array, a negative number which is the bitwise complement of (the index of the last element plus 1).

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type . When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).

Exceptions

 

Exception Condition
ArgumentNullException array is null.
ArgumentOutOfRangeException index is less than the lower bound of array.-or-length is less than zero.
ArgumentException index and length do not specify a valid range in array.-or-value is of a type that is not compatible with the elements of array.
InvalidOperationException value does not implement the IComparable(T) generic interface, and the search encounters an element that does not implement the IComparable(T) generic interface.

See Also

Reference

ArrayExtensions Class
BinarySearch Overload
CodeJam.Collections Namespace

Clone this wiki locally