UnmanagedMemory 2.2.0
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package UnmanagedMemory --version 2.2.0
NuGet\Install-Package UnmanagedMemory -Version 2.2.0
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="UnmanagedMemory" Version="2.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="UnmanagedMemory" Version="2.2.0" />
<PackageReference Include="UnmanagedMemory" />
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 UnmanagedMemory --version 2.2.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: UnmanagedMemory, 2.2.0"
#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 UnmanagedMemory@2.2.0
#: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=UnmanagedMemory&version=2.2.0
#tool nuget:?package=UnmanagedMemory&version=2.2.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
UnmanagedMemory
A .NET library for safe and efficient allocation and management of unmanaged memory, with a modern, idiomatic API. Designed for high-performance scenarios where direct memory control is required.
Features
- Safe Unmanaged Memory Allocation: Allocate, free, and work with unmanaged memory using familiar .NET patterns.
- Span<T> Support: Expose unmanaged memory as
Span<T>
for fast, safe access. - LINQ Integration: Use LINQ queries directly on unmanaged memory buffers.
- Disposal Pattern: Implements
IDisposable
for deterministic cleanup. - Unsafe Operations: Allows advanced scenarios with pointers when needed.
- .NET Standard 2.1+: Broad compatibility across .NET platforms.
Getting Started
Installation
Install via NuGet:
dotnet add package UnmanagedMemory
Basic Usage
using UnmanagedMemory;
// Allocate unmanaged memory for 10 integers
using var memory = new UnsafeMemory<int>(10);
// Fill the memory with a value
memory.AsSpan().Fill(25);
// Set a specific element
memory[9] = 20;
// LINQ query on the buffer
var filtered = memory.Where(x => x > 20);
Console.WriteLine($"[{string.Join(", ", filtered)}]");
// Access raw pointer (unsafe context)
unsafe
{
var ptr = memory.AsUnsafePointer();
}
API Overview
UnsafeMemory<T>
A disposable wrapper for a block of unmanaged memory.
new UnsafeMemory<T>(int length)
: Allocates memory forlength
elements.Span<T> AsSpan()
: Access the memory as a span.T this[int index]
: Indexer for element access.- Implements
IEnumerable<T>
for LINQ support.
Unmanaged
Static helpers for raw memory allocation.
IntPtr Malloc(int bytes)
: Allocates a block of unmanaged memory.void Free(ref IntPtr ptr)
: Frees unmanaged memory and nulls the pointer.
Example
using var memory = new UnsafeMemory<float>(100);
memory.AsSpan().Fill(3.14f);
foreach (var value in memory.Where(x => x > 3.0f))
Console.WriteLine(value);
NativeStringBuilder
using var nativeBuilder = new NativeStringBuilder();
nativeBuilder
.Append("Hello")
.Append(',')
.Append(" ")
.Append("World")
.Append('!');
var txt = nativeBuilder.ToString();
FileUtility
using UnsafeMemory<byte> bytes = await FileUtility.ReadAllBytesAsync("/path/to/file");
// Use the bytes.
Requirements
- .NET Standard 2.1 or later (.NET Core 3.0+, .NET 5+, .NET 6+, .NET 7+, .NET 8+, .NET 9+)
License
MIT © BosonWare, Technologies
Links
Release Notes
Refactor memory management and enhance UnsafeMemory API
- Replaces MemoryUtils with improved memory management functionality
- Adds memory expansion capabilities and safer pointer handling
- Improves debug support with DebuggerTypeProxy
- Enhances span operations with new AsSpan overloads
- Adds explicit operators for Span conversion
- Updates documentation for clarity and completeness
Breaking changes:
- This Version contains no breaking changes.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. 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. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.1
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Added UnmanagedTypes:
- NativeStringBuilder
Import documentation.