GenericExtensionsBinarySearchROT, TKey(IReadOnlyListT, FuncT, TKey, TKey) Method

Searches the entire sorted IReadOnlyListT for an element and returns the zero-based index of the element.

If the key is not found, a negative number is returned, which can be intepreted as the bitwise complement of the interval of indices that the key is in between, i.e. list[interval-1] < key < list[interval]

This differs from the "ordinary" binary search in allowing a keySelectorcomparer that knows how to compare a class with its key. Example, if the list contains classes of type T having an id number and the class is sorted on that id, then the keySelector returns the id number for that class.

Definition

Namespace: DHI.Mike1D.Generic
Assembly: DHI.Mike1D.Generic (in DHI.Mike1D.Generic.dll) Version: 24.0.0.0 (11.1.1.1111)
C#
public static int BinarySearchRO<T, TKey>(
	this IReadOnlyList<T> list,
	Func<T, TKey> keySelector,
	TKey key
)

Parameters

list  IReadOnlyListT
List to search in
keySelector  FuncT, TKey
A function to extract a key from an element.
key  TKey
The key to locate. The value can be null for reference types.

Type Parameters

T
The type of the objects in the list
TKey
The type of the argument to look for

Return Value

Int32
The zero-based index of item in the sorted IListT, if item is found; otherwise, a negative number that is the bitwise complement of the index of the next element that is larger than item or, if there is no larger element, the bitwise complement of Count.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type IReadOnlyListT. 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).

See Also