Source: Microsoft Support
RAPID PUBLISHING ARTICLES PROVIDE INFORMATION DIRECTLY FROM WITHIN THE MICROSOFT SUPPORT ORGANIZATION. THE INFORMATION CONTAINED HEREIN IS CREATED IN RESPONSE TO EMERGING OR UNIQUE TOPICS, OR IS INTENDED SUPPLEMENT OTHER KNOWLEDGE BASE INFORMATION.
You are developing a .NET Framework application using an instance of System.Text.StringBuilder, whose Capacity has been initialized to a specific number of characters. You then call the StringBuilder.Insert method to insert a string at a specified location within range of the Capacity, similar to the code below.
Visual Basic .NET
===============
    Sub Main()
       Dim strTest As New System.Text.StringBuilder(11)
       strTest.Insert(3, "Hello")
       Console.WriteLine(strTest.ToString)
   End Sub
C# .NET
======
   static void Main(string[] args)
       {
           System.Text.StringBuilder strTest = new StringBuilder(11);
           strTest.Insert(3, "Hello");
           Console.WriteLine(strTest.ToString());
       }
Â
You receive the following exception and call stack.
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
  at System.Text.StringBuilder.Insert(Int32 index, String value, Int32 count)
  at ConsoleApplication1.Program.Main(String[] args)Â
  at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
  at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
  at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
  at System.Threading.ThreadHelper.ThreadStart()
The Index specifed in the Insert method is larger than the current length of the StringBuilder. The Capacity property specifies the maximum number of characters that can be contained within the memory currently allocated by the StringBuilder. It does not initialize the StringBuilder to a particular Length. In the example above, the code is attempting to insert a string at Index 3. Since the StringBuilder does not currently contain any data, you can only insert a string at Index 0.
This behavior is by design.
Before calling the Insert method, make sure the StringBuilder's Length property is greater than or equal to the Index you will be using in the Insert method. You can initialize the StringBuilder to contain a sufficient characters in its constructor, or you can use the Append method to add characters to the StringBuilder. For example, the following code add three spaces to the StringBuilder, which will then allow you to begin inserting at Index 3.
Visual Basic .NET
=============
   strTest.Append(" "c,3) 'Append 3 spaces
   strTest.Insert(3, "Hello")
C# .NET
======
   strTest.Append((char)32,3); //Append 3 spaces
   strTest.Insert(3, "Hello");
MICROSOFT AND/OR ITS SUPPLIERS MAKE NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY, RELIABILITY OR ACCURACY OF THE INFORMATION CONTAINED IN THE DOCUMENTS AND RELATED GRAPHICS PUBLISHED ON THIS WEBSITE (THE “MATERIALSâ€) FOR ANY PURPOSE. THE MATERIALS MAY INCLUDE TECHNICAL INACCURACIES OR TYPOGRAPHICAL ERRORS AND MAY BE REVISED AT ANY TIME WITHOUT NOTICE.
TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, MICROSOFT AND/OR ITS SUPPLIERS DISCLAIM AND EXCLUDE ALL REPRESENTATIONS, WARRANTIES, AND CONDITIONS WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT LIMITED TO REPRESENTATIONS, WARRANTIES, OR CONDITIONS OF TITLE, NON INFRINGEMENT, SATISFACTORY CONDITION OR QUALITY, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, WITH RESPECT TO THE MATERIALS.