title: Version of C# StringBuilder to allow for strings larger than 2 billion characters
body: <p>In C#, <strong>64bit Windows</strong> + <strong>.NET 4.5 (or later)</strong> + <strong>enabling <code>gcAllowVeryLargeObjects</code> in the App.config file</strong> allows for objects larger than two gigabyte. That's cool, but unfortunately, the maximum number of elements that C# allows in a character array is still <a href="https://docs.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/runtime/gcallowverylargeobjects-element" rel="nofollow noreferrer">limited to about 2^31 = 2.15 billion chars</a>. Testing confirmed this.</p>
  
  <p>To overcome this, Microsoft <a href="https://web.archive.org/web/20181119090643/https://blogs.msdn.microsoft.com/joshwil/2005/08/10/bigarrayt-getting-around-the-2gb-array-size-limit/" rel="nofollow noreferrer">recommends in Option B</a> creating the arrays natively (their 'Option C' doesn't even compile). That suits me, as speed is also a concern. Is there some tried and trusted unsafe / native / interop / PInvoke code for .NET out there that can replace and act as an enhanced StringBuilder to get around the 2 billion element limit?</p>
  
  <p>Unsafe/pinvoke code is preferred, but not a deal breaker. Alternatively, is there a .NET (safe) version available?</p>
  
  <p>Ideally, the StringBuilder replacement will start off small (preferably user-defined), and then repeatedly double in size each time the capacity has been exceeded. I'm mostly looking for <code>append()</code> functionality here. Saving the string to a file would be useful too, though I'm sure I could program that bit if <code>substring()</code> functionality is also incorporated. If the code uses pinvoke, then obviously some degree of memory management must be taken into account to avoid memory loss.</p>
  
  <p>I don't want to recreate the wheel if some simple code already exists, but on the other hand, I don't want to download and incorporate a DLL just for this simple functionality.</p>
  
  <p>I'm also using .NET 3.5 to cater for users who don't have the latest version of Windows.</p>