This question is regarding types Span<T>
and RealOnlySpan<T>
in .NET Core. The documentation explains how to create a span over an array, or an array segment, or a stack-allocated or unmanaged memory. In some circumstances I would like to create a span of length 1 over a single variable (a field, a ref
parameter, or ref
local) to pass it to API that can deal with arbitrary spans. How can I do that?
I imagine there could be a constructor Span<T>.Span(ref T)
, but apparently there isn't one. Copying the variable into an array and creating a span over that array does not solve the problem, because any modifications of the span would not reflect on the original variable.
ref T
points to at least one element, you have a bigger problem elsewhere), but that the result's lifetime is not tied to thereference
's ‒ the span is not protected from leaking. – Backwash