List<'T>.BinarySearch Method (Int32, Int32, 'T, IComparer<'T>)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Searches a range of elements in the sorted List<'T> for an element using the specified comparer and returns the zero-based index of the element.

Namespace:   System.Collections.Generic
Assembly:  mscorlib (in mscorlib.dll)

member BinarySearch : 
        index:int *
        count:int *
        item:'T *
        comparer:IComparer<'T> -> int


Type: System.Int32

The zero-based starting index of the range to search.

Type: System.Int32

The length of the range to search.

Type: T

The object to locate. The value can be null for reference types.

Type: System.Collections.Generic.IComparer<'T>

The IComparer<'T> implementation to use when comparing elements, or null to use the default comparer Comparer<'T>.Default.

Return Value

Type: System.Int32

The zero-based index of item in the sorted List<'T>, 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.

Exception Condition

index is less than 0.


count is less than 0.


index and count do not denote a valid range in the List<'T>.


comparer is null, and the default comparer Comparer<'T>.Default cannot find an implementation of the IComparable<'T> generic interface or the IComparable interface for type T.

The comparer customizes how the elements are compared. For example, you can use a CaseInsensitiveComparer instance as the comparer to perform case-insensitive string searches.

If comparer is provided, the elements of the List<'T> are compared to the specified value using the specified IComparer<'T> implementation.

If comparer is null, the default comparer Comparer<'T>.Default checks whether type T implements the IComparable<'T> generic interface and uses that implementation, if available. If not, Comparer<'T>.Default checks whether type T implements the IComparable interface. If type T does not implement either interface, Comparer<'T>.Default throws InvalidOperationException.

The List<'T> must already be sorted according to the comparer implementation; otherwise, the result is incorrect.

Comparing null with any reference type is allowed and does not generate an exception when using the IComparable<'T> generic interface. When sorting, null is considered to be less than any other object.

If the List<'T> contains more than one element with the same value, the method returns only one of the occurrences, and it might return any one of the occurrences, not necessarily the first one.

If the List<'T> does not contain the specified value, the method returns a negative integer. You can apply the bitwise complement operation (~) to this negative integer to get the index of the first element that is larger than the search value. When inserting the value into the List<'T>, this index should be used as the insertion point to maintain the sort order.

This method is an O(log n) operation, where n is the number of elements in the range.

The following example demonstrates the Sort(Int32, Int32, IComparer<'T>) method overload and the BinarySearch(Int32, Int32, 'T, IComparer<'T>) method overload.

The example defines an alternative comparer for strings named DinoCompare, which implements the IComparer<string> (IComparer(Of String) in Visual Basic, IComparer<String^> in Visual C++) generic interface. The comparer works as follows: First, the comparands are tested for null, and a null reference is treated as less than a non-null. Second, the string lengths are compared, and the longer string is deemed to be greater. Third, if the lengths are equal, ordinary string comparison is used.

A List<'T> of strings is created and populated with the names of five herbivorous dinosaurs and three carnivorous dinosaurs. Within each of the two groups, the names are not in any particular sort order. The list is displayed, the range of herbivores is sorted using the alternate comparer, and the list is displayed again.

The BinarySearch(Int32, Int32, 'T, IComparer<'T>) method overload is then used to search only the range of herbivores for "Brachiosaurus". The string is not found, and the bitwise complement (the ~ operator in C# and Visual C++, Xor -1 in Visual Basic) of the negative number returned by the BinarySearch(Int32, Int32, 'T, IComparer<'T>) method is used as an index for inserting the new string.

No code example is currently available or this language may not be supported.

Universal Windows Platform
Available since 8
.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top