Closed
Description
openedon May 11, 2019
Version Used:
c# 7.3
Steps to Reproduce:
public ref struct Matrix<T> where T: unmanaged
{
private readonly int w;
private readonly int h;
private readonly Span<T> span;
public Matrix(int w, int h)
{
this.w = w;
this.h = h;
this.span = stackalloc T[w*h];
}
results in error
CS8353: A result of a stackalloc expression of type 'Span<T>' cannot be used in this context because it may be exposed outside of the containing method
I think I understand the error message and why it is necessary to prevent it.
But in this specific case the this.span is a field on a ref struct itself, so the stack will be maintained and the matrix itself cannot "be exposed outside the context".
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment