How to handle out of memory exception string builder
<p>I have this method that is expected to return a string but the string is pretty big maybe in GB. Currently this runs into out of memory exception. </p>

<p>I was thinking I would write to a file (random filename) then read it delete the file and send back the response. Tried <code>MemoryStream</code> and <code>StreamWriter</code> that too ran into out of memory exception. </p>

<p>Did not want to initialize <code>StringBuilder</code> with a capacity since the size was not known and I think it needs a continuous block of memory.</p>

<p>What is the best course of action to solve this problem?</p>

<pre><code>    public string Result()
    {
        StringBuilder response = new StringBuilder();
        for (int i = 1; i &lt;= Int.Max; i++)
        {
            response.Append("keep adding some text {0}", i);
            response.Append(Environment.NewLine);
        }

        return response.ToString();
    }
</code></pre>