Class ArrayUtil
Provides utility methods for working with arrays, including dynamic resizing, capacity management, searching, sorting, and element manipulation.
Inherited Members
Namespace: Scylla.Core.Util
Assembly: ScyllaCore.dll
Syntax
public static class ArrayUtil
Remarks
This class is designed for scenarios where arrays are used as backing stores that need to grow on demand without incurring frequent small reallocations. Growth strategies follow an amortized doubling pattern: when the current length is insufficient, the backing array is grown to at least twice its current size (or to the required minimum, whichever is larger). This keeps the average cost of a growth operation O(1) amortized.
Most methods treat a null or zero-length array as equivalent to an empty
backing store, allocating a new array when necessary rather than throwing. Exceptions
are only raised for logically invalid arguments such as negative indices or sizes.
For higher-level collection operations - such as shuffling, dequeuing, or priority
ordering - see CollectionUtil and the data-structure types under
Scylla.Core.Structures.
Fields
DefaultCapacity
The default initial capacity used when allocating a new backing array without an explicit
minimum size. Applied by EnsureCapacity<T>(ref T[], int) and ResizeIfNecessary<T>(ref T[], int)
when the input array is null or empty and no larger minimum is required.
Declaration
public const int DefaultCapacity = 10
Field Value
| Type | Description |
|---|---|
| int |
Methods
Clear<T>(T[])
Clears all elements in the array to their default value without reallocating. For reference types, this sets elements to null. For value types, this sets elements to their default value.
Declaration
public static void Clear<T>(T[] array)
Parameters
| Type | Name | Description |
|---|---|---|
| T[] | array | The array to clear. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of elements in the array. |
Clear<T>(T[], int, int)
Clears a range of elements in the array to their default value without reallocating.
Declaration
public static void Clear<T>(T[] array, int startIndex, int count)
Parameters
| Type | Name | Description |
|---|---|---|
| T[] | array | The array to clear. |
| int | startIndex | The starting index of the range to clear. |
| int | count | The number of elements to clear. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of elements in the array. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentOutOfRangeException | Thrown if startIndex is negative, count is negative, or the range exceeds the array bounds. |
Contains<T>(T[], T)
Determines whether the array contains the specified element.
Declaration
public static bool Contains<T>(T[] array, T item)
Parameters
| Type | Name | Description |
|---|---|---|
| T[] | array | The array to search. |
| T | item | The item to search for. |
Returns
| Type | Description |
|---|---|
| bool | True if the item is found, otherwise false. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of elements in the array. |
CopyTo<T>(T[], ref T[])
Copies elements from the source array to the destination array. The destination array will be resized if necessary.
Declaration
public static void CopyTo<T>(T[] source, ref T[] destination)
Parameters
| Type | Name | Description |
|---|---|---|
| T[] | source | The source array to copy from. |
| T[] | destination | A reference to the destination array. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of elements in the arrays. |
Copy<T>(T[])
Creates a shallow copy of the array.
Declaration
public static T[] Copy<T>(T[] source)
Parameters
| Type | Name | Description |
|---|---|---|
| T[] | source | The source array to copy. |
Returns
| Type | Description |
|---|---|
| T[] | A new array containing copies of all elements, or null if source is null. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of elements in the array. |
CountWhere<T>(T[], Predicate<T>)
Counts the number of elements in the array that match the conditions defined by the specified predicate.
Declaration
public static int CountWhere<T>(T[] array, Predicate<T> match)
Parameters
| Type | Name | Description |
|---|---|---|
| T[] | array | The array to search. |
| Predicate<T> | match | The predicate that defines the conditions to count. |
Returns
| Type | Description |
|---|---|
| int | The number of elements that match the predicate. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of elements in the array. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown if match is null. |
EnsureCapacity<T>(ref T[], int)
Ensures the array has at least the specified minimum capacity, growing it if necessary while preserving existing elements.
Declaration
public static void EnsureCapacity<T>(ref T[] array, int minCapacity)
Parameters
| Type | Name | Description |
|---|---|---|
| T[] | array | A reference to the array to conditionally grow. May be |
| int | minCapacity | The minimum number of elements the array must be able to hold after this call. Must be non-negative. |
Type Parameters
| Name | Description |
|---|---|
| T | The element type stored in the array. |
Remarks
When the current array length is less than minCapacity, the array
is reallocated to max(array.Length * 2, minCapacity), amortizing the cost of
repeated calls. Existing elements are preserved by Array.Resize.
If the array is null or empty, a new array is allocated to
max(DefaultCapacity, minCapacity) so that at least
DefaultCapacity slots are available for future additions.
Exceptions
| Type | Condition |
|---|---|
| ArgumentOutOfRangeException | Thrown when |
Exists<T>(T[], Predicate<T>)
Determines whether the array contains elements that match the conditions defined by the specified predicate.
Declaration
public static bool Exists<T>(T[] array, Predicate<T> match)
Parameters
| Type | Name | Description |
|---|---|---|
| T[] | array | The array to search. |
| Predicate<T> | match | The predicate that defines the conditions to search for. |
Returns
| Type | Description |
|---|---|
| bool | True if one or more elements match the predicate, otherwise false. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of elements in the array. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown if match is null. |
Fill<T>(T[], T)
Fills the entire array with the specified value.
Declaration
public static void Fill<T>(T[] array, T value)
Parameters
| Type | Name | Description |
|---|---|---|
| T[] | array | The array to fill. |
| T | value | The value to fill the array with. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of elements in the array. |
Fill<T>(T[], T, int, int)
Fills a range of elements in the array with the specified value.
Declaration
public static void Fill<T>(T[] array, T value, int startIndex, int count)
Parameters
| Type | Name | Description |
|---|---|---|
| T[] | array | The array to fill. |
| T | value | The value to fill with. |
| int | startIndex | The starting index of the range to fill. |
| int | count | The number of elements to fill. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of elements in the array. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentOutOfRangeException | Thrown if startIndex is negative, count is negative, or the range exceeds the array bounds. |
FindAll<T>(T[], Predicate<T>)
Retrieves all elements that match the conditions defined by the specified predicate.
Declaration
public static T[] FindAll<T>(T[] array, Predicate<T> match)
Parameters
| Type | Name | Description |
|---|---|---|
| T[] | array | The array to search. |
| Predicate<T> | match | The predicate that defines the conditions to search for. |
Returns
| Type | Description |
|---|---|
| T[] | An array containing all elements that match the conditions, or an empty array if no matches are found. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of elements in the array. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown if match is null. |
FindIndex<T>(T[], Predicate<T>)
Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the first occurrence within the array.
Declaration
public static int FindIndex<T>(T[] array, Predicate<T> match)
Parameters
| Type | Name | Description |
|---|---|---|
| T[] | array | The array to search. |
| Predicate<T> | match | The predicate that defines the conditions to search for. |
Returns
| Type | Description |
|---|---|
| int | The zero-based index of the first occurrence, or -1 if no match is found. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of elements in the array. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown if match is null. |
FindLastIndex<T>(T[], Predicate<T>)
Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the last occurrence within the array.
Declaration
public static int FindLastIndex<T>(T[] array, Predicate<T> match)
Parameters
| Type | Name | Description |
|---|---|---|
| T[] | array | The array to search. |
| Predicate<T> | match | The predicate that defines the conditions to search for. |
Returns
| Type | Description |
|---|---|
| int | The zero-based index of the last occurrence, or -1 if no match is found. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of elements in the array. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown if match is null. |
Find<T>(T[], Predicate<T>)
Searches for an element that matches the conditions defined by the specified predicate, and returns the first occurrence within the array.
Declaration
public static T Find<T>(T[] array, Predicate<T> match)
Parameters
| Type | Name | Description |
|---|---|---|
| T[] | array | The array to search. |
| Predicate<T> | match | The predicate that defines the conditions to search for. |
Returns
| Type | Description |
|---|---|
| T | The first element that matches the conditions, or the default value of T if no match is found. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of elements in the array. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown if match is null. |
GrowBy<T>(ref T[], int)
Grows the array by the specified additional capacity.
Declaration
public static void GrowBy<T>(ref T[] array, int additionalCapacity)
Parameters
| Type | Name | Description |
|---|---|---|
| T[] | array | A reference to the array to grow. |
| int | additionalCapacity | The number of additional elements to add to the capacity. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of elements in the array. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentOutOfRangeException | Thrown if additionalCapacity is negative. |
IndexOf<T>(T[], T)
Searches for the specified element and returns the index of its first occurrence.
Declaration
public static int IndexOf<T>(T[] array, T item)
Parameters
| Type | Name | Description |
|---|---|---|
| T[] | array | The array to search. |
| T | item | The item to search for. |
Returns
| Type | Description |
|---|---|
| int | The zero-based index of the first occurrence, or -1 if not found. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of elements in the array. |
LastIndexOf<T>(T[], T)
Searches for the specified element and returns the index of its last occurrence.
Declaration
public static int LastIndexOf<T>(T[] array, T item)
Parameters
| Type | Name | Description |
|---|---|---|
| T[] | array | The array to search. |
| T | item | The item to search for. |
Returns
| Type | Description |
|---|---|
| int | The zero-based index of the last occurrence, or -1 if not found. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of elements in the array. |
ResizeIfNecessary<T>(ref T[], int)
Ensures the array is large enough to be indexed at requiredIndex,
growing it if the current length is insufficient.
Declaration
public static void ResizeIfNecessary<T>(ref T[] array, int requiredIndex)
Parameters
| Type | Name | Description |
|---|---|---|
| T[] | array | A reference to the array to conditionally grow. May be |
| int | requiredIndex | The zero-based index that must be a valid position after this call. Must be non-negative. |
Type Parameters
| Name | Description |
|---|---|
| T | The element type stored in the array. |
Remarks
When growth is needed, the new length is set to requiredLength * 2 where
requiredLength = requiredIndex + 1, providing room for subsequent writes
without immediately requiring another reallocation.
If the array is null or empty, a new array is allocated to
max(DefaultCapacity, requiredLength + DefaultCapacity).
Exceptions
| Type | Condition |
|---|---|
| ArgumentOutOfRangeException | Thrown when |
Reverse<T>(T[])
Reverses the order of all elements in the array.
Declaration
public static void Reverse<T>(T[] array)
Parameters
| Type | Name | Description |
|---|---|---|
| T[] | array | The array to reverse. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of elements in the array. |
Reverse<T>(T[], int, int)
Reverses the order of elements in the specified range of the array.
Declaration
public static void Reverse<T>(T[] array, int startIndex, int count)
Parameters
| Type | Name | Description |
|---|---|---|
| T[] | array | The array containing elements to reverse. |
| int | startIndex | The starting index of the range to reverse. |
| int | count | The number of elements to reverse. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of elements in the array. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentOutOfRangeException | Thrown if startIndex is negative, count is negative, or the range exceeds the array bounds. |
Sort<T>(T[])
Sorts the elements in the array using the default comparer.
Declaration
public static void Sort<T>(T[] array) where T : IComparable<T>
Parameters
| Type | Name | Description |
|---|---|---|
| T[] | array | The array to sort. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of elements in the array. Must implement IComparable<T>. |
Sort<T>(T[], IComparer<T>)
Sorts the elements in the array using the specified comparer.
Declaration
public static void Sort<T>(T[] array, IComparer<T> comparer)
Parameters
| Type | Name | Description |
|---|---|---|
| T[] | array | The array to sort. |
| IComparer<T> | comparer | The comparer to use when comparing elements. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of elements in the array. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown if comparer is null. |
Sort<T>(T[], Comparison<T>)
Sorts the elements in the array using the specified comparison delegate.
Declaration
public static void Sort<T>(T[] array, Comparison<T> comparison)
Parameters
| Type | Name | Description |
|---|---|---|
| T[] | array | The array to sort. |
| Comparison<T> | comparison | The comparison delegate to use when comparing elements. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of elements in the array. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown if comparison is null. |
Sort<T>(T[], int, int)
Sorts a range of elements in the array using the default comparer.
Declaration
public static void Sort<T>(T[] array, int startIndex, int count) where T : IComparable<T>
Parameters
| Type | Name | Description |
|---|---|---|
| T[] | array | The array containing the range to sort. |
| int | startIndex | The starting index of the range to sort. |
| int | count | The number of elements to sort. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of elements in the array. Must implement IComparable<T>. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentOutOfRangeException | Thrown if startIndex is negative, count is negative, or the range exceeds the array bounds. |
Swap<T>(T[], int, int)
Swaps two elements at the specified indices in the array.
Declaration
public static void Swap<T>(T[] array, int indexA, int indexB)
Parameters
| Type | Name | Description |
|---|---|---|
| T[] | array | The array containing elements to swap. |
| int | indexA | The index of the first element. |
| int | indexB | The index of the second element. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of elements in the array. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown if array is null. |
| ArgumentOutOfRangeException | Thrown if either index is out of bounds. |
TrimExcess<T>(ref T[], int)
Shrinks the array to the exact size needed, removing excess capacity. This is useful when the array has been pre-allocated with extra capacity that is no longer needed.
Declaration
public static void TrimExcess<T>(ref T[] array, int usedCount)
Parameters
| Type | Name | Description |
|---|---|---|
| T[] | array | A reference to the array to trim. |
| int | usedCount | The number of elements actually in use. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of elements in the array. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentOutOfRangeException | Thrown if usedCount is negative. |