Skip to content

M_CodeJam_Collections_ArrayExtensions_BinarySearch__1_3

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

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

Searches an entire one-dimensional sorted Array for a value using the specified IComparer(T) generic interface.

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,
	IComparer<T> comparer
)

VB

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

F#

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

Parameters

 

array
Type: T[]
The sorted one-dimensional, zero-based Array to search.
value
Type: T
The object to search for.
comparer
Type: System.Collections.Generic.IComparer(T)
The IComparer(T) implementation to use when comparing elements.-or- null to use the IComparable(T) implementation of each element.

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.
ArgumentException comparer is null, and value is of a type that is not compatible with the elements of array.
InvalidOperationException comparer is null, 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