Developer knowledge network · moderated exchange

UnreliableCode қауымдастығы

Әзірлеушілерді зерттеу, кері инженерия және кодтау қауымдастығы

Knowledge indexТірі
4Categories
919Threads
2.8KЖазбалар
Tutorial

Using Ref Fields in Ref Structs (C# 11) for high-performance memory abstractions [StackOverflow Architecture Guide]

dotnet_runtime_architect
.NET Core Specialist
MEMBER
Өкіл: 103
Қосылу күні: Apr 2018
Хабарламалар: 40
Рахмет: 24
2 апта бұрын · Aug 7, 2026 11:53 PM
#1

How C# 11 enabled ref fields inside ref struct types:

CSHARP
public ref struct CustomSpanReader<T>
{
    private ref T _current; // Ref field pointing directly into stack/heap memory
    private int _length;
    
    public CustomSpanReader(Span<T> span)
    {
        _current = ref MemoryMarshal.GetReference(span);
        _length = span.Length;
    }
}

Allows building custom enumerators, parsing streams, and windowed buffer scanners with direct pointer-like performance while maintaining 100% memory safety.

unsafe_memory_wizard
Unsafe & P/Invoke
MEMBER
Өкіл: 160
Қосылу күні: Jul 2022
Хабарламалар: 10
Рахмет: 38
2 апта бұрын · Aug 8, 2026 4:12 AM
#2

ref fields made it possible to write custom Span-like types in standard C# without dropping into unsafe pointer code.

profiler_pat
Performance Hunter
MEMBER
Өкіл: 146
Қосылу күні: Aug 2019
Хабарламалар: 33
Рахмет: 31
2 апта бұрын · Aug 8, 2026 2:49 PM
#3

A major feature for high-performance parser and network protocol library maintainers.