Struct UnsafeSpan

Public

Replacement for System.Span which is compatible with earlier versions of C#.

Warning

These spans do not in any way guarantee that the memory they refer to is valid. It is up to the user to make sure the memory is not deallocated before usage. It should never be used to refer to managed heap memory without pinning it, since unpinned managed memory can be moved by some runtimes.

This has several benefits over e.g. UnsafeList:

  • It is faster to index into a span than into an UnsafeList, especially from C#. In fact, indexing into an UnsafeSpan is as fast as indexing into a native C# array.

    • As a comparison, indexing into a NativeArray can easily be 10x slower, and indexing into an UnsafeList is at least a few times slower.

  • You can create a UnsafeSpan from a C# array by pinning it.

  • It can be sliced efficiently.

  • It supports ref returns for the indexing operations.

Public Methods

Clone (allocator)

Creates a new copy of the span allocated using the given allocator.

Public
CopyTo (other)

Copies the memory of this span to another span.

Public
Free (allocator)

Frees the underlaying memory.

Public
Slice (start)

Creates a new span which is a slice of this span.

Public
Slice (start, length)

Creates a new span which is a slice of this span.

Public
ToArray ()

Converts the span to a managed array.

Public
UnsafeSpan (ptr, length)
Public
UnsafeSpan (data, gcHandle)

Creates a new UnsafeSpan from a C# array.

Public
UnsafeSpan (allocator, length)

Allocates a new UnsafeSpan with the specified length.

Public

Public Variables

Length

Number of elements in this span.

Public
this[int index]
Public
this[uint index]
Public

Private/Protected Members

length
Internal Readonly
ptr
Internal Readonly