美文网首页c#学习
C# stackalloc 函数

C# stackalloc 函数

作者: 李药师_hablee | 来源:发表于2019-12-24 21:37 被阅读0次

    贴代码

    using System;
    
    namespace ConsoleApp3
    {
        class Program
        {
            unsafe static string IntToString(int value)
            {
                char* buffer = stackalloc char[16];
                char* p = buffer + 16;
                int n = value >= 0 ? value : -value;
                do
                {
                    *--p = (char)(n % 10 + '0');
                    n /= 10;
                } while (n != 0);
                if (value < 0)
                    *--p = '-';
                return new string(p, 0, (int)(buffer + 16 - p));
            }
            static void Main(string[] args)
            {
                Console.WriteLine(IntToString(12345));
                Console.WriteLine(IntToString(-999));
                //Console.WriteLine("Hello World!");
            }
        }
    }
    

    遇到编译问题,请参考这篇文章

    相关文章

      网友评论

        本文标题:C# stackalloc 函数

        本文链接:https://www.haomeiwen.com/subject/oztqoctx.html