allocates 26 characters on the stack the for loop assigns alphabetic characters to each element
using System; public unsafe class Starter { public static void Main() { char* pChar = stackalloc char[26]; char* _pChar = pChar; for (int count = 0; count < 26; ++count) { (*_pChar) = (char)(((int)('A')) + count); ++_pChar; } for (int count = 0; count < 26; ++count) { Console.Write(pChar[count]); } } }
1. | stackalloc Demo | ||
2. | Allocating Memory from the Stack | ||
3. | Invalid Cast Exceptions with Implicit Operators | ||
4. | Use stackalloc to allocate memory for integer array |