ImmutableList<T>.Builder.BinarySearch Method

Definition

Overloads

BinarySearch(T)

Searches the entire ImmutableList<T>.Builder for an element using the default comparer and returns the zero-based index of the element.

BinarySearch(T, IComparer<T>)

Searches the entire ImmutableList<T>.Builder for an element using the specified comparer and returns the zero-based index of the element.

BinarySearch(Int32, Int32, T, IComparer<T>)

Searches the specified range of the ImmutableList<T>.Builder for an element using the specified comparer and returns the zero-based index of the element.

BinarySearch(T)

Source:
ImmutableList_1.Builder.cs
Source:
ImmutableList_1.Builder.cs
Source:
ImmutableList_1.Builder.cs
Source:
ImmutableList_1.Builder.cs

Searches the entire ImmutableList<T>.Builder for an element using the default comparer and returns the zero-based index of the element.

C#
public int BinarySearch(T item);

Parameters

item
T

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

Returns

The zero-based index of item in the ImmutableList<T>.Builder, 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.

Exceptions

The default comparer Default cannot find an implementation of the IComparable<T> generic interface or the IComparable interface for type T.

Remarks

If the ImmutableList<T>.Builder 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 ImmutableList<T>.Builder, this index should be used as the insertion point to maintain the sort order.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8 (package-provided), 8, 9 (package-provided), 9
.NET Framework 4.7 (package-provided), 4.7.1 (package-provided), 4.7.2 (package-provided), 4.8 (package-provided)
.NET Standard 2.0 (package-provided)
UWP 10.0

BinarySearch(T, IComparer<T>)

Source:
ImmutableList_1.Builder.cs
Source:
ImmutableList_1.Builder.cs
Source:
ImmutableList_1.Builder.cs
Source:
ImmutableList_1.Builder.cs

Searches the entire ImmutableList<T>.Builder for an element using the specified comparer and returns the zero-based index of the element.

C#
public int BinarySearch(T item, System.Collections.Generic.IComparer<T> comparer);
C#
public int BinarySearch(T item, System.Collections.Generic.IComparer<T>? comparer);

Parameters

item
T

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

comparer
IComparer<T>

The implementation to use when comparing elements, or null for the default comparer.

Returns

The zero-based index of item in the ImmutableList<T>.Builder, 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.

Exceptions

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

Remarks

If the ImmutableList<T>.Builder 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 ImmutableList<T>.Builder, this index should be used as the insertion point to maintain the sort order.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8 (package-provided), 8, 9 (package-provided), 9
.NET Framework 4.7 (package-provided), 4.7.1 (package-provided), 4.7.2 (package-provided), 4.8 (package-provided)
.NET Standard 2.0 (package-provided)
UWP 10.0

BinarySearch(Int32, Int32, T, IComparer<T>)

Source:
ImmutableList_1.Builder.cs
Source:
ImmutableList_1.Builder.cs
Source:
ImmutableList_1.Builder.cs
Source:
ImmutableList_1.Builder.cs

Searches the specified range of the ImmutableList<T>.Builder for an element using the specified comparer and returns the zero-based index of the element.

C#
public int BinarySearch(int index, int count, T item, System.Collections.Generic.IComparer<T> comparer);
C#
public int BinarySearch(int index, int count, T item, System.Collections.Generic.IComparer<T>? comparer);

Parameters

index
Int32

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

count
Int32

The length of the range to search.

item
T

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

comparer
IComparer<T>

The implementation to use when comparing elements, or null for the default comparer.

Returns

The zero-based index of item in the ImmutableList<T>.Builder, 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.

Exceptions

index is less than 0. -or-

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 Default cannot find an implementation of the IComparable<T> generic interface or the IComparable interface for type T.

Remarks

If the ImmutableList<T>.Builder 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 ImmutableList<T>.Builder, this index should be used as the insertion point to maintain the sort order.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8 (package-provided), 8, 9 (package-provided), 9
.NET Framework 4.7 (package-provided), 4.7.1 (package-provided), 4.7.2 (package-provided), 4.8 (package-provided)
.NET Standard 2.0 (package-provided)
UWP 10.0