MemoryExtensions.BinarySearch Method

Definition

Overloads

BinarySearch<T,TComparable>(ReadOnlySpan<T>, TComparable)

Searches an entire sorted ReadOnlySpan<T> for a value using the specified TComparable generic type.

BinarySearch<T,TComparable>(Span<T>, TComparable)

Searches an entire sorted Span<T> for a value using the specified TComparable generic type.

BinarySearch<T,TComparer>(Span<T>, T, TComparer)

Searches an entire sorted Span<T> for a specified value using the specified TComparer generic type.

BinarySearch<T,TComparer>(ReadOnlySpan<T>, T, TComparer)

Searches an entire sorted ReadOnlySpan<T> for a specified value using the specified TComparer generic type.

BinarySearch<T>(ReadOnlySpan<T>, IComparable<T>)

Searches an entire sorted ReadOnlySpan<T> for a value using the specified IComparable<T> generic interface.

BinarySearch<T>(Span<T>, IComparable<T>)

Searches an entire sorted Span<T> for a value using the specified IComparable<T> generic interface.

BinarySearch<T,TComparable>(ReadOnlySpan<T>, TComparable)

Source:
MemoryExtensions.cs
Source:
MemoryExtensions.cs
Source:
MemoryExtensions.cs

Searches an entire sorted ReadOnlySpan<T> for a value using the specified TComparable generic type.

C#
public static int BinarySearch<T,TComparable>(this ReadOnlySpan<T> span, TComparable comparable) where TComparable : IComparable<T>;

Type Parameters

T

The element type of the span.

TComparable

The specific type of IComparable<T>.

Parameters

span
ReadOnlySpan<T>

The sorted ReadOnlySpan<T> to search.

comparable
TComparable

The TComparable to use when comparing.

Returns

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

Exceptions

comparable is null.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 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), 2.1

BinarySearch<T,TComparable>(Span<T>, TComparable)

Source:
MemoryExtensions.cs
Source:
MemoryExtensions.cs
Source:
MemoryExtensions.cs

Searches an entire sorted Span<T> for a value using the specified TComparable generic type.

C#
public static int BinarySearch<T,TComparable>(this Span<T> span, TComparable comparable) where TComparable : IComparable<T>;

Type Parameters

T

The element type of the span.

TComparable

The specific type of IComparable<T>.

Parameters

span
Span<T>

The sorted Span<T> to search.

comparable
TComparable

The TComparable to use when comparing.

Returns

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

Exceptions

comparable is null.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 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), 2.1

BinarySearch<T,TComparer>(Span<T>, T, TComparer)

Source:
MemoryExtensions.cs
Source:
MemoryExtensions.cs
Source:
MemoryExtensions.cs

Searches an entire sorted Span<T> for a specified value using the specified TComparer generic type.

C#
public static int BinarySearch<T,TComparer>(this Span<T> span, T value, TComparer comparer) where TComparer : System.Collections.Generic.IComparer<T>;

Type Parameters

T

The element type of the span.

TComparer

The specific type of IComparer<T>.

Parameters

span
Span<T>

The sorted Span<T> to search.

value
T

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

comparer
TComparer

The TComparer to use when comparing.

Returns

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

Exceptions

comparer is null.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 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), 2.1

BinarySearch<T,TComparer>(ReadOnlySpan<T>, T, TComparer)

Source:
MemoryExtensions.cs
Source:
MemoryExtensions.cs
Source:
MemoryExtensions.cs

Searches an entire sorted ReadOnlySpan<T> for a specified value using the specified TComparer generic type.

C#
public static int BinarySearch<T,TComparer>(this ReadOnlySpan<T> span, T value, TComparer comparer) where TComparer : System.Collections.Generic.IComparer<T>;

Type Parameters

T

The element type of the span.

TComparer

The specific type of IComparer<T>.

Parameters

span
ReadOnlySpan<T>

The sorted ReadOnlySpan<T> to search.

value
T

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

comparer
TComparer

The TComparer to use when comparing.

Returns

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

Exceptions

comparer is null.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 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), 2.1

BinarySearch<T>(ReadOnlySpan<T>, IComparable<T>)

Source:
MemoryExtensions.cs
Source:
MemoryExtensions.cs
Source:
MemoryExtensions.cs

Searches an entire sorted ReadOnlySpan<T> for a value using the specified IComparable<T> generic interface.

C#
public static int BinarySearch<T>(this ReadOnlySpan<T> span, IComparable<T> comparable);

Type Parameters

T

The element type of the span.

Parameters

span
ReadOnlySpan<T>

The sorted ReadOnlySpan<T> to search.

comparable
IComparable<T>

The IComparable<T> to use when comparing.

Returns

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

Exceptions

comparable is null.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 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), 2.1

BinarySearch<T>(Span<T>, IComparable<T>)

Source:
MemoryExtensions.cs
Source:
MemoryExtensions.cs
Source:
MemoryExtensions.cs

Searches an entire sorted Span<T> for a value using the specified IComparable<T> generic interface.

C#
public static int BinarySearch<T>(this Span<T> span, IComparable<T> comparable);

Type Parameters

T

The element type of the span.

Parameters

span
Span<T>

The sorted Span<T> to search.

comparable
IComparable<T>

The IComparable<T> to use when comparing.

Returns

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

Exceptions

comparable is null.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 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), 2.1