Export (0) Print
Expand All

Do not concatenate strings inside loops

.NET Framework 2.0

TypeName

DoNotConcatenateStringsInsideLoops

CheckId

CA1818

Category

Microsoft.Performance

Breaking Change

NonBreaking

A method iteratively builds a string inside an iteration statement, such as a for or while loop, using the System.String.Concat method or using the addition (+ or &) or addition assignment (+=) operators.

A System.String object is immutable. When two strings are concatenated, a new String object is created. Iterative string concatenation creates multiple strings that are un-referenced and must be garbage collected. For better performance, use the System.Text.StringBuilder class.

StringBuilder maintains a mutable character buffer, which allows string concatenation and other changes to occur without the overhead of object creation and garbage collection. The initial capacity of the StringBuilder is an important performance consideration. When the capacity is no longer sufficient to hold the string, the original buffer is discarded and another buffer is created with twice the capacity of the original.

To fix a violation of this rule, use an instance of the StringBuilder class to build the string inside the iteration statement. After the string is built, use the System.Text.StringBuilder.ToString method to access the string.

Exclude a warning from this rule if the temporary strings are used inside the iteration statement. In this case, StringBuilder does not offer a performance gain because its ToString method also creates a new String object on each iteration.

The following example shows four violations of this rule, one of which should be excluded, and a method, Loop5, which satisfies the rule using a StringBuilder.

using System;
using System.Globalization;
using System.Text;

namespace PerformanceLibrary
{
   class MakeString
   {
      static CultureInfo currentCulture = CultureInfo.CurrentCulture;

      static void Main()
      {
         // Loops 1 through 4 violate this rule.
         Loop1();
         Loop2();
         Loop3();
         Loop4();
         Loop5();
      }

      static void Loop1()
      {
         string digits = string.Empty;
         for(int i = 0; i < 10; i++)
         {
            digits = String.Concat(digits, i.ToString(currentCulture));
         }
         Console.WriteLine(digits);
      }
      
      // This method violates the rule but 
      // the violation should be excluded.
      static void Loop2()
      {
         string digits = string.Empty;
         for(int i = 0; i < 10; i++)
         {
            digits = String.Concat(digits, i.ToString(currentCulture));
            Console.WriteLine(digits);
         }
      }

      static void Loop3()
      {
         string digits = string.Empty;
         for(int i = 0; i < 10; i++)
         {
            digits = digits + i.ToString(currentCulture);
         }
         Console.WriteLine(digits);
      }

      static void Loop4()
      {
         string digits = string.Empty;
         for(int i = 0; i < 10; i++)
         {
            digits += i.ToString(currentCulture);
         }
         Console.WriteLine(digits);
      }

      // This method does not cause a violation of the rule.
      static void Loop5()
      {
         StringBuilder digits = new StringBuilder();
         for(int i = 0; i < 10; i++)
         {
            digits.Append(i.ToString(currentCulture));
         }
         Console.WriteLine(digits.ToString());
      }
   }
}

Community Additions

ADD
Show:
© 2014 Microsoft