'Can I suppress .local for stackalloc in C# without using unsafe?

To allocate a double array initialized to zero, you can choose

var x = new double[N];

to allocate on the heap and

Span<double> x = stackalloc double[N];

to allocate on the stack.

To suppress initialization to zero, you can call

var x = GC.AllocateUninitializedArray<double>(N);

to allocate on the heap, but I don't know how to do the same thing on the stack. You can use the SkipLocalsInit property, but that then requires compiling in unsafe. Is there a way to achieve this result but not use unsafe?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source