What is the maximum possible length of a .NET string?

后端 未结 8 1824
梦毁少年i
梦毁少年i 2020-11-22 02:48

What is the longest string that can be created in .NET? The docs for the String class are silent on this question as far as I can see, so an authoritative answe

8条回答
  •  忘掉有多难
    2020-11-22 03:25

    200 megs... at which point your app grinds to a virtual halt, has about a gig working set memory, and the o/s starts to act like you'll need to reboot.

    static void Main(string[] args)
    {
        string s = "hello world";
        for(;;)
        {
            s = s + s.Substring(0, s.Length/10);
            Console.WriteLine(s.Length);
        }
    }
    
    12
    13
    14
    15
    16
    17
    18
    ...
    158905664
    174796230
    192275853
    211503438
    

提交回复
热议问题