CategoriesMemory & Performance

Memory & Performance

6 features across 4 C# versions

C# 72017

Lightweight Value Tuples

Enables multi-value assignment returns directly via stack structures, completely eliminating object allocation overhead.

1public (int x, int y) MeasureCoordinates() => (1920, 1080);
2var (width, height) = MeasureCoordinates();
C# 72017

Span<T> Optimization Hooks (C# 7.2)

Exposes memory-safe contiguous slices of arbitrary memory layout arrays (stack, heap, or native) entirely free from structural copying penalty.

1ReadOnlySpan<char> span = "High performance processing context".AsSpan();
2ReadOnlySpan<char> slice = span.Slice(0, 4);
C# 72017

Ref Struct Declarations (C# 7.2)

Enforces that a structural object variant type live exclusively inside stack limits, eliminating heap migration patterns entirely.

1public ref struct StackBufferBuffer {
2 public Span<byte> Data;
3}
C# 112022

Ref Fields inside Ref Structs

Permits ref struct instances to internally preserve references pointing to scoped memory locations, enhancing processing performance.

1public ref struct SegmentReader {
2 public ref readonly byte CurrentByte;
3}
C# 122023

Inline Arrays Allocation Control

Enables structures to construct fixed-size arrays embedded cleanly inside a stack boundary, delivering high performance without heap allocations.

1[System.Runtime.CompilerServices.InlineArray(10)]
2public struct DirectBuffer {
3 private int _elementZero;
4}
C# 132024

Flexible Params Collections

Allows the params keyword modifier to accept arbitrary collection types—such as memory-optimized Span elements—bypassing allocation penalties.

1public void LogEvents(params ReadOnlySpan<string> messages) {
2 foreach (var msg in messages) Console.WriteLine(msg);
3}