GenericExtensionsBinarySearchT Method (IListT, FuncT, Int32) |
Searches the entire sorted
IListT for an element using the provided
comparer and returns the zero-based index of the element.
This differs from the "ordinary" binary search in allowing a comparer delegate that defines
whether an item is found (returning 0), whether the item in the list is before (<0) or after (>0)
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.
Examples
If having a list of doubles, to find 4.5 in the list, use:
int index = list.BinarySearch(d => d.CompareTo(4.5))
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>(
this IList<T> list,
Func<T, int> comparer
)
<ExtensionAttribute>
Public Shared Function BinarySearch(Of T) (
list As IList(Of T),
comparer As Func(Of T, Integer)
) As Integer
public:
[ExtensionAttribute]
generic<typename T>
static int BinarySearch(
IList<T>^ list,
Func<T, int>^ comparer
)
Parameters
- list
- Type: System.Collections.GenericIListT
List to search in - comparer
- Type: SystemFuncT, Int32
A delegate/lambda expression specifying if a given item in the list is before (<0), matches (0) or is after (>0) the item to search for.
Type Parameters
- T
- The type of the objects in the list
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