Developer knowledge network · moderated exchange

Społeczność UnreliableCode

Badania programistów, inżynieria wsteczna i społeczność programistów

Knowledge indexNa żywo
4Categories
919Threads
2.8KPosty
Tutorial

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

dotnet_runtime_architect
.NET Core Specialist
MEMBER
Rozpustnik: 103
Data dołączenia: Apr 2018
Posty: 40
Dzięki: 24
2 tygodnie temu · 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
Rozpustnik: 160
Data dołączenia: Jul 2022
Posty: 10
Dzięki: 38
2 tygodnie temu · 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
Rozpustnik: 146
Data dołączenia: Aug 2019
Posty: 33
Dzięki: 31
2 tygodnie temu · Aug 8, 2026 2:49 PM
#3

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