Arrays

Array representation, System::Array static helpers, ArraySegment, Buffer, and Span.

The Array Type: std::vector<T>

In sharp-runtime, C# T[] (single-dimensional arrays) map to std::vector<T>. There is no custom array object type. Most ported code that uses int[], byte[], or string[] will use std::vector<intcs>, std::vector<bytecs>, or std::vector<std::string> respectively.

System::Array — Static Helper Class

include/System/Array.hpp

System::Array is a static template class — it cannot be instantiated. It provides static template helpers operating on std::vector<T>.

Status: Partial

MethodDescription
Sort(vector<T>&)Sorts in-place using default comparer
Sort(vector<T>&, comparison)Sorts using a custom std::function<int(T,T)>
Copy(src, srcIndex, dst, dstIndex, length)Copies a range between vectors
Copy(T* src, srcIndex, T* dst, dstIndex, length)Copies between raw C-arrays via memcpy
Resize(vector<T>&, intcs newSize)Resizes vector, preserving elements
IndexOf(const vector<T>&, const T&)First index of element, or -1
Reverse(vector<T>&)Reverses in-place
Clear(vector<T>&, intcs index, intcs length)Sets range to default value
using namespace System;
using SharpRuntime::intcs;

std::vector<intcs> v = {3, 1, 4, 1, 5, 9};
Array::Sort(v);              // {1, 1, 3, 4, 5, 9}
Array::Reverse(v);           // {9, 5, 4, 3, 1, 1}
intcs idx = Array::IndexOf(v, 4); // 2
Array::Resize(v, 4);         // {9, 5, 4, 3}
Array::Clear(v, 0, 2);       // {0, 0, 4, 3}

ArraySegment<T>

include/System/ArraySegment.hpp

A struct representing a view into a portion of an array (vector). Provides Array, Offset, Count members.

Buffer

include/System/Buffer.hpp

Header-only static utility class for raw byte-level operations:

Span<T> and ReadOnlySpan<T>

include/System/Span.hpp

A non-owning view over a contiguous region of memory, similar to C# Span<T>. Supports:

std::vector<int> data = {1, 2, 3, 4, 5};
System::Span<int> span(data.data(), data.size());
auto sub = span.Slice(1, 3);  // view of {2, 3, 4}

Collections (Higher Level)

For richer collection semantics (adding/removing elements, dictionaries, etc.), see the collections module:

See Collections Module.