Skip to content

OnixLabs.Core.ReadOnlySpanExtensions

Matthew Layton edited this page Nov 15, 2026 · 1 revision

ReadOnlySpanExtensions Class

Provides extension methods for ReadOnlySpan<byte> and ReadOnlySpan<char> instances, computing content-based hash codes without allocating.

Namespace OnixLabs.Core
Assembly OnixLabs.Core.dll (NuGet)
Inheritance object → ReadOnlySpanExtensions

Definition

[EditorBrowsable(EditorBrowsableState.Never)]
public static class ReadOnlySpanExtensions

Note This class is decorated with [EditorBrowsable(EditorBrowsableState.Never)], which keeps the container class itself out of IntelliSense lists while its extension methods remain fully usable on the receiver types. Bring the methods into scope with using OnixLabs.Core;.

Remarks

ReadOnlySpanExtensions uses C# 14 extension blocks (extension(ReadOnlySpan<byte> receiver) and extension(ReadOnlySpan<char> receiver)), so the receiver type appears in the block declaration rather than as a this parameter; the signatures shown below are the call-site signatures as consumed by user code.

GetContentHashCode hashes the content of a span rather than its reference, several bytes at a time and without allocating. Spans with equal content produce equal hash codes. Like HashCode, the result is randomised per process and must not be persisted.

The library's byte- and character-backed value types — Base16, Base32, Base58 and Base64, and Hash, Salt, DigitalSignature, PublicKey and SecurityToken in the security packages — compute their GetHashCode results with these methods.

Methods

Method Returns Description
GetContentHashCode() (ReadOnlySpan<byte>) int Gets the content hash code of the bytes in the current span.
GetContentHashCode() (ReadOnlySpan<char>) int Gets the ordinal content hash code of the characters in the current span.

GetContentHashCode()

public int GetContentHashCode()

Gets the content hash code of the bytes in the current ReadOnlySpan<byte>. Spans with equal content produce equal hash codes. The bytes are hashed several at a time, without allocating. Like HashCode, the result is randomised per process and must not be persisted.

Returns — The content hash code of the bytes in the current span.

using OnixLabs.Core;

ReadOnlySpan<byte> left = [0x01, 0x02, 0x03];
ReadOnlySpan<byte> right = [0x01, 0x02, 0x03];

bool equal = left.GetContentHashCode() == right.GetContentHashCode(); // true

GetContentHashCode()

public int GetContentHashCode()

Gets the ordinal content hash code of the characters in the current ReadOnlySpan<char>. Spans with ordinally equal content produce equal hash codes. The characters are hashed several at a time, without allocating. Like HashCode, the result is randomised per process and must not be persisted.

Returns — The ordinal content hash code of the characters in the current span.

using OnixLabs.Core;

int hash = "Hello".AsSpan().GetContentHashCode();

See Also

ONIXLabs .NET Library


Clone this wiki locally