MaxCapacity of StringBuilder

Viewed 17991

why does this code throw an OutOfMemoryException on i = 690864192?

StringBuilder sb = new StringBuilder();
                for (int i = 0; i < Int32.MaxValue; i++)
                {
                    sb.Append("s");
                }
                Console.WriteLine(sb.ToString());
                Console.Read();

The default capacity is 16 chars, but this grows at it needs to up to the max which is int.MaxValue = 2,147,483,647. So why is it when number of chars is 690,864,192 which is much less than the max capacity, does it throw an exception?

2 Answers
Related