Skip to content

M_CodeJam_Collections_ArrayExtensions_BinarySearch__1_2

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

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

Searches an entire one-dimensional sorted Array for a specific element, using the IComparable(T) generic interface implemented by each element of the Array and by the specified object.

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

Syntax

C#

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

VB

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

F#

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

Parameters

 

array
Type: T[]
The sorted one-dimensional, zero-based Array 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.
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