In C#, a stackalloc
keyword is used to allocate a block of memory on the stack. It is only used in an unsafe code context.
Starting with C# 7.2, you can assign the result of a stackalloc
expression to either System.Span<T>
or System.ReadOnlySpan<T>
without using an unsafe context.
Span<int> evenNumbers = stackalloc int[10];
for (int i = 0; i < 10; i++)
{
evenNumbers[i] = i*2;
}
for (int i = 0; i < 10; i++)
{
Console.WriteLine(evenNumbers[i]);
}
You can use a stackalloc
expression in conditional or assignment expressions as shown below.
int length = 250;
Span<byte> bytes = length <= 256 ? stackalloc byte[length] : new byte[length];
public unsafe static void Example3()
{
int* oddNumbers = stackalloc int[5] { 1, 3, 5, 7, 9 };
for (int i = 0; i < 5; i++)
{
Console.WriteLine(oddNumbers[i]);
}
}
In C# 7.3, you can also use a pointer type for initializing arrays in an unsafe context as shown below.
int* oddNumbers = stackalloc int[5] { 1, 3, 5, 7, 9 };
for (int i = 0; i < 5; i++)
{
Console.WriteLine(oddNumbers[i]);
}