GenericExtensionsBinarySearchT, TKey Method (IListT, FuncT, TKey, TKey) |
Searches the entire sorted
IListT 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.
Namespace:
DHI.Mike1D.Generic
Assembly:
DHI.Mike1D.Generic (in DHI.Mike1D.Generic.dll) Version: 19.0.0.0 (11.1.1.1111)
Syntax public static int BinarySearch<T, TKey>(
this IList<T> list,
Func<T, TKey> keySelector,
TKey key
)
<ExtensionAttribute>
Public Shared Function BinarySearch(Of T, TKey) (
list As IList(Of T),
keySelector As Func(Of T, TKey),
key As TKey
) As Integer
public:
[ExtensionAttribute]
generic<typename T, typename TKey>
static int BinarySearch(
IList<T>^ list,
Func<T, TKey>^ keySelector,
TKey key
)
Parameters
- list
- Type: System.Collections.GenericIListT
List to search in - keySelector
- Type: SystemFuncT, TKey
A function to extract a key from an element. - key
- Type: 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
Type:
Int32The 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
IListT. 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