SimulationTree.Unmanaged.Core 0.3.9

Prefix Reserved
dotnet add package SimulationTree.Unmanaged.Core --version 0.3.9
                    
NuGet\Install-Package SimulationTree.Unmanaged.Core -Version 0.3.9
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SimulationTree.Unmanaged.Core" Version="0.3.9" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SimulationTree.Unmanaged.Core" Version="0.3.9" />
                    
Directory.Packages.props
<PackageReference Include="SimulationTree.Unmanaged.Core" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add SimulationTree.Unmanaged.Core --version 0.3.9
                    
#r "nuget: SimulationTree.Unmanaged.Core, 0.3.9"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package SimulationTree.Unmanaged.Core@0.3.9
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=SimulationTree.Unmanaged.Core&version=0.3.9
                    
Install as a Cake Addin
#tool nuget:?package=SimulationTree.Unmanaged.Core&version=0.3.9
                    
Install as a Cake Tool

Unmanaged

Test

Library containing primitives for working with native C#.

Installation

Install it by cloning it, or referencing through the NuGet package through GitHub's registry (authentication info).

For installing as a Unity package, use this git url to add it:

https://github.com/simulation-tree/unmanaged.git?path=core#unity

Memory Addresses

MemoryAddress instances can point to either heap or stack memory. They can be allocated using static methods, in which case they must also be disposed:

//an allocation that is 10 bytes in size
using (MemoryAddress allocation = MemoryAddress.Allocate(sizeof(char) * 5))
{
    allocation.Write("Hello".AsSpan());
    Span<char> text = allocation.AsSpan<char>();
}

//an allocation containing a float
using (MemoryAddress allocation = MemoryAddress.Create(3.14f))
{
    ref float floatValue = ref allocation.Read<float>();
    floatValue *= 2;
}

ASCII Text

The types prefixed with ASCIIText store extended ASCII char values. Useful for when text is known to be short, until a list/array is needed:

ASCIIText16 text = new("Hello there");
Span<char> textBuffer = stackalloc char[text.Length];
text.CopyTo(textBuffer);

//get utf8 bytes from the text
Span<byte> utf8bytes = stackalloc char[ASCIIText16.Capacity];
uint bytesCopied = text.CopyTo(utf8bytes);

//get text from utf8 bytes using System.Text.Encoding
ASCIIText1024 textFromBytes = new(utf8bytes.Slice(0, bytesCopied));
string systemString = Encoding.UTF8.GetString(textBuffer.Slice(0, length));
Assert.That(textFromBytes.ToString(), Is.EqualTo(systemString));

//casting to a smaller type
ASCIIText32 textFromBytesButSmaller = textFromBytes;
Assert.That(textFromBytesButSmaller.ToString(), Is.EqualTo(systemString));

Text

Accompanying the above is the disposable Text type, which is a reference to an arbitrary amount of chars values. And behaves like a string:

using Text builder = new();
builder.Append("Hello");
builder.Append(" there");

ReadOnlySpan<char> text = builder.AsSpan();
Console.WriteLine(text.ToString());

Random Generator

Can generate random data and values using the XORshift technique:

using RandomGenerator random = new();
int fairDiceRoll = random.NextInt(0, 6);

The default random seed is based on process ID, current time, and instance index.

Safety

When working with MemoryAddress values, there are checks available to ensure that the memory is not accessed after it has been disposed, or accessed out of bounds. This only occurs in debug mode, or with the #TRACK directive defined, and only on heap allocated memory through the MemoryAddress methods.

Ultimately, it is the programmer's responsibility for how memory should be managed. Including when allocations should be disposed, and how they should be accessed.

Included default analyzer

Included is an analyzer that emits errors where a disposable struct type is being created, but then initialized to default. Because disposable types imply they have a way to properly initialize them:

Allocation allocation = default; //U0001 error

There is no analysis for new() however, because a default constructor with value types can be by design. Though if not, they can be declared with an [Obsolete("", true)] attribute with the parameter true to disallow it.

Contributing and direction

This library is made to provide building blocks for working with native code in C#. For minimizing runtime cost and to expose the efficiency that was always there. Commonly putting the author in a position where they need to exercise more control.

with great power, comes great responsibility

Contributions that fit this are welcome.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net9.0

    • No dependencies.

NuGet packages (20)

Showing the top 5 NuGet packages that depend on SimulationTree.Unmanaged.Core:

Package Downloads
SimulationTree.Unmanaged

The building blocks of native C# projects

SimulationTree.Worlds.Core

Native ECS library

SimulationTree.Shaders

Package Description

SimulationTree.Meshes

Package Description

SimulationTree.Data.Core

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.3.9 985 9/24/2025
0.3.8 180 9/24/2025
0.3.6 341 9/15/2025
0.3.5 263 9/14/2025
0.3.4 248 9/14/2025
0.3.3 254 9/14/2025