Skip to content

OnixLabs.Core.ArrayExtensions

Matthew Layton edited this page Nov 15, 2026 · 2 revisions

ArrayExtensions Class

Extension methods for arrays, including generic-array copying and concatenation, and a byte[]-to-string conversion.

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

Definition

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

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

ArrayExtensions provides helpers that operate on generic T[] arrays and on byte[] specifically. The copy methods produce new arrays rather than mutating the receiver. Several of these methods use C# 14 extension blocks (extension(Receiver 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.

Methods

Method Returns Description
Copy<T>() T[] Creates an exact copy of the current array.
Copy<T>(int, int) T[] Creates a copy of a contiguous range of the current array.
ConcatenateWith<T>(T[]) T[] Concatenates the current array with another array.
ToString(Encoding) string Decodes the current byte[] to a string using the specified encoding.

Copy<T>()

public T[] Copy<T>()

Creates a copy of the current T array.

Returns — An exact copy of the current T array.

int[] source = [1, 2, 3];
int[] clone = source.Copy(); // [1, 2, 3] (new array)

Copy<T>(int, int)

public T[] Copy<T>(int index, int count)

Creates a copy of a contiguous range of the current T array.

Parameter Type Description
index int The index of the array to begin copying from.
count int The number of elements of the array to copy.

Returns — A new T array containing the specified range of elements from the current array.

Exceptions

Exception Condition
ArgumentOutOfRangeException index and count fall outside the bounds of the array.
int[] source = [10, 20, 30, 40, 50];
int[] middle = source.Copy(1, 3); // [20, 30, 40]

ConcatenateWith<T>(T[])

public T[] ConcatenateWith<T>(T[] other)

Concatenates the current T array with another T array.

Parameter Type Description
other T[] The other T array to concatenate with the current array.

Returns — A new T array consisting of the current array followed by other.

byte[] head = [0x01, 0x02];
byte[] tail = [0x03, 0x04];
byte[] all = head.ConcatenateWith(tail); // [0x01, 0x02, 0x03, 0x04]

ToString(Encoding)

public string ToString(Encoding encoding)

Obtains the string representation of the current byte[] array, decoded with the specified encoding.

Parameter Type Description
encoding Encoding The Encoding used to convert the current byte[] into a string.

Returns — The string representation of the current byte[] array.

byte[] bytes = [0x48, 0x65, 0x6C, 0x6C, 0x6F];
string text = bytes.ToString(Encoding.UTF8); // "Hello"

See Also

ONIXLabs .NET Library


Clone this wiki locally