sidristij/dotnetbook

stackalloc, value types, unmanaged types

qbit86 opened this issue · 0 comments

MemorySpan.md mentions that stackalloc requires type to be value type: «Однако, конечно, TType должен быть только значимым типом, т.к. stackalloc работает только со значимыми типами.»

But this is not enough; TType has to be unmanaged type. For example, ArraySegment<int> is value type, but at the same time it is also managed type (since it contains reference):

Span<ArraySegment<int>> bad = stackalloc ArraySegment<int>[count];

Error CS0208: Cannot take the address of, get the size of, or declare a pointer to a managed type ('ArraySegment')