System::Array

Static array helper class. The actual array type is std::vector<T>.

include/System/Array.hpp

Status: Partial

Key Design Choice
In sharp-runtime, C# T[] maps to std::vector<T>, not to System::Array. The System::Array class contains only static template helpers that operate on std::vector. Its constructor is deleted — you cannot instantiate it.

Overview

When porting C# code that uses Array.Sort(arr) or Array.Copy(src, dst, len), replace with System::Array::Sort(vec) or System::Array::Copy(src, dst, len).

The static methods are all template functions that accept std::vector<T>&.

Static Template Methods

Sort

template<typename T>
static void Sort(std::vector<T>& array);

template<typename T, typename Comparison>
static void Sort(std::vector<T>& array, Comparison comparison);

Sorts the vector in-place. Wraps std::sort. The two-argument overload accepts a comparator (equivalent to C# IComparer).

std::vector<int> nums = {3, 1, 2};
System::Array::Sort(nums);
// nums is now {1, 2, 3}

System::Array::Sort(nums, [](int a, int b){ return a > b; });
// nums is now {3, 2, 1}

Copy

template<typename T>
static void Copy(const std::vector<T>& src, std::vector<T>& dst, int length);

template<typename T>
static void Copy(const std::vector<T>& src, int srcIndex,
                 std::vector<T>& dst, int dstIndex, int length);

Copies elements between vectors. The four-index overload copies a range with offset.

Resize

template<typename T>
static void Resize(std::vector<T>& array, int newSize);

Resizes the vector. Elements beyond the new size are dropped; new slots are value-initialized. Wraps std::vector::resize.

IndexOf

template<typename T>
static int IndexOf(const std::vector<T>& array, const T& value);

template<typename T>
static int IndexOf(const std::vector<T>& array, const T& value, int startIndex);

Returns the zero-based index of the first occurrence of value, or -1 if not found.

Reverse

template<typename T>
static void Reverse(std::vector<T>& array);

Reverses the vector in-place. Wraps std::reverse.

Clear

template<typename T>
static void Clear(std::vector<T>& array, int index, int length);

Sets elements in the range [index, index+length) to their default value (zero for numeric types, empty for strings, etc.).

BinarySearch

template<typename T>
static int BinarySearch(const std::vector<T>& array, const T& value);

Binary search on a sorted vector. Returns the index or a negative value if not found (same convention as .NET). Needs verification

Mapping from C#

C# codesharp-runtime equivalent
int[] arr = new int[5];std::vector<int> arr(5);
int[] arr = {1,2,3};std::vector<int> arr = {1,2,3};
arr.Lengtharr.size()
arr[i]arr[i] (same)
Array.Sort(arr)System::Array::Sort(arr)
Array.Copy(src,dst,n)System::Array::Copy(src,dst,n)
Array.Resize(ref arr, n)System::Array::Resize(arr, n)
Array.IndexOf(arr, val)System::Array::IndexOf(arr, val)
Array.Reverse(arr)System::Array::Reverse(arr)
Array.Clear(arr, i, n)System::Array::Clear(arr, i, n)

Related Types