here is my code line:
StringBuilder myCompleteMessage = new StringBuilder();
myCompleteMessage.Capacity = Int32.MaxValue-1;
tryed that also:
myCompleteMessage.Capacity = myCompleteMessage.MaxCapacity-1;
I get exception on line 2.
Exception of type 'System.OutOfMemoryException' was thrown.
Stack Trace:
at System.String.GetStringForStringBuilder(String value, Int32 startIndex, Int32 length, Int32 capacity)
at System.Text.StringBuilder.set_Capacity(Int32 value)
at Orca.Server.HandleClientComm(Object newClient) in C:\Users\Dan\Documents\Visual Studio 2010\Projects\msig\Orca\Server.cs:line 100
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart(Object obj)
Assuming you are on a 32bit system, that second line will always fail. You’re asking .NET to allocate 4 GB of space to your StringBuilder, which is more memory than the process has to work with (thanks to Joel for pointing out char takes up 2 bytes, not 1).
EDIT
If you take a look at
StringBuilderwith ILSpy, you see this bit of code in the set forCapacity:By setting Capacity to int.MaxValue – 1, you’re telling .NET to try and allocate a 4 GB char array, which is why the code is failing.