Arrays, Spans, and Memory
Separate ownership, algorithms, and borrowed contiguous views before translating C# array code.
Owned sequences, algorithms, and views
Sharp Runtime maps most C# one-dimensional array work to an owned std::vector<T>, familiar System::Array algorithms, and explicit borrowed views. Those pieces are intentionally separate: an algorithm class should not pretend to own a vector, and a view should not pretend to retain its owner.
Selection guide
| If you need… | Use… | Remember… |
|---|---|---|
| Owned resizable contiguous elements | std::vector<T> | Growth can relocate storage |
| Familiar sort/copy/search/clear helpers | System::Array | Static algorithms over vectors/raw buffers |
| Synchronous writable borrow | Span<T> | Owner must outlive it |
| Synchronous read-only borrow | ReadOnlySpan<T> | Read-only through this alias only |
| Offset/count vector view | ArraySegment<T> | Default segment and mutation have edge cases |
| Managed-style Memory name | Memory<T> | Still non-owning in this implementation |
| Collection interfaces/versioned enumeration | List-shaped collection | Element ownership remains a C++ choice |
Key differences from C#
- There is no universal CLR
System.Arraybase object or runtime covariance. - Native
vector[]is unchecked; Span and runtime helpers add explicit checks. - A vector can resize, so views and references can become invalid.
- Copying follows the exact C++ element type, including smart-pointer or move-only semantics.
- Current Memory and ArraySegment types borrow their vector instead of retaining managed storage.
- Rectangular multidimensional arrays need a deliberate nested, flat, matrix, or domain representation.
Core component
Array, Span, Memory, and ArraySegment are public entries in Core.Base. Select it directly for a narrow consumer:
set(SHARP_RUNTIME_COMPONENTS Core.Base)
set(SHARP_RUNTIME_BUILD_TESTS OFF CACHE BOOL "" FORCE)
add_subdirectory(sharp-runtime)
target_link_libraries(app PRIVATE SharpRuntime::Core.Base)
Detailed documentation
System::Array referenceSort, copy, resize, search, clear, fill, conversion, and predicate families.
Array internalsOwnership, bounds, overlap, views, invalidation, multidimensional choices, and porting.
Collections tutorialChoose sequence and element ownership together.
Type systemValue, reference, nullable, Object, and view relationships.
Safety invariant
A view is never the owner
Do not retain Span, Memory, ArraySegment, iterators, references, or raw pointers after owner destruction or vector relocation. Current Memory::Pin exposes a pointer; it does not retain or immobilize native storage.