Table of Contents

Struct NativeArray<T>

Namespace
SharpPluginLoader.Core
Assembly
SharpPluginLoader.Core.dll

A wrapper around a native array. This type acts similar to a Span<T>, but can be stored in a field or property, and can take ownership of the memory it points to.

public struct NativeArray<T> : IEnumerable<T>, IEnumerable, IDisposable where T : unmanaged

Type Parameters

T
Implements
Inherited Members

Constructors

NativeArray(nint, int)

Creates a new native array from a given address and count.

public NativeArray(nint address, int length)

Parameters

address nint

The address of the first element

length int

The number of elements in the array

NativeArray(nint, int, IAllocator)

Creates a new native array from a given address and count.

public NativeArray(nint address, int length, IAllocator allocator)

Parameters

address nint

The address of the first element

length int

The number of elements in the array

allocator IAllocator

The allocator to use

Remarks

Use this constructor if you want the native array to take ownership of the memory it points to.

Properties

Address

The address of the first element in the array.

public readonly nint Address { get; }

Property Value

nint

this[int]

Gets a reference to the element at the specified index.

public readonly ref T this[int index] { get; }

Parameters

index int

The index to get the element from

Property Value

T

A reference to the element at the provided index

Length

The number of elements in the array.

public readonly int Length { get; }

Property Value

int

Pointer

A pointer to the first element in the array.

public readonly T* Pointer { get; }

Property Value

T*

Methods

AsSpan()

Creates a span over the native array.

public readonly Span<T> AsSpan()

Returns

Span<T>

A span over the native array.

Create(int, IAllocator?)

Creates a new native array from a given address and count.

public static NativeArray<T> Create(int length, IAllocator? allocator = null)

Parameters

length int

The number of elements

allocator IAllocator

The allocator to use. A null value uses the default allocator.

Returns

NativeArray<T>

The newly allocated native array

Remarks

Warning: The memory allocated by this method is not automatically freed. You must call Dispose() when you are done with the array. Alternatively, use a using statement to ensure that the array is disposed.

Dispose()

Disposes the native array.

public readonly void Dispose()

GetEnumerator()

Returns an enumerator that iterates through the collection.

public IEnumerator<T> GetEnumerator()

Returns

IEnumerator<T>

An enumerator that can be used to iterate through the collection.

Resize(int)

Resizes the native array.

public void Resize(int newLength)

Parameters

newLength int

The new size

Remarks

Warning: This method can only be called on NativeArrays that were created using the Create(int, IAllocator?) method.

Slice(int, int)

public readonly NativeArray<T> Slice(int start, int newLength)

Parameters

start int
newLength int

Returns

NativeArray<T>