Developer knowledge network · moderated exchange

UnreliableCode コミュニティ

開発者リサーチ、リバース エンジニアリング、コーディング コミュニティ

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.