can anypone explain me string builder and string with good example ?

From the help file for string builder under "Performance Considerations":

The performance of a concatenation operation for a String or StringBuilder object depends on how often a memory allocation occurs. A String concatenation operation always allocates memory, whereas a StringBuilder concatenation operation only allocates memory if the StringBuilder object buffer is too small to accommodate the new data. Consequently, the String class is preferable for a concatenation operation if a fixed number of String objects are concatenated. In that case, the individual concatenation operations might even be combined into a single operation by the compiler. A StringBuilder object is preferable for a concatenation operation if an arbitrary number of strings are concatenated; for example, if a loop concatenates a random number of strings of user input.

Execution Time: 20234.375

private void simpleButton1_Click(object sender, EventArgs e)
    {
      string s = new string('x', 6000);
      DateTime dtStart = DateTime.Now;
      string result = string.Empty;
      for (int i1 = 0; i1 < 1900; i1++)
      {
        result += s;
      }
      DateTime dtEnd = DateTime.Now;

      double milliSecTaken = dtEnd.Subtract(dtStart).TotalMilliseconds;
      Console.WriteLine(milliSecTaken);
    }

Execution Time: 62.5

private void simpleButton1_Click(object sender, EventArgs e)
    {
      string s = new string('x', 6000);
      DateTime dtStart = DateTime.Now;
      string result = string.Empty;
      StringBuilder sb = new StringBuilder();
      for (int i1 = 0; i1 < 1900; i1++)
      {
        sb.AppendLine(s);
      }
      result = sb.ToString();
      DateTime dtEnd = DateTime.Now;

      double milliSecTaken = dtEnd.Subtract(dtStart).TotalMilliseconds;
      Console.WriteLine(milliSecTaken); 
    }

Having to reallocate a new string which is increasing in size every iteration of the loop kills performance in the first code snippet. The second code snippet uses a string builder and does not suffer from the same performance degredation as the first snippet.

Thanks sknake, Informative thread.

I would like to add few things:

StringBuilder represents a mutable string of characters - it can be modified once it has been created by appending, removing, replacing, or inserting characters.

String objects are immutable. Its value cannot be modified once it has been created.

in the above example u were used both string an dstring builder as well..which on is best..as my knowlegde stringbuilder is good..explain me with good understanding example with brief detail plz

StringBuilder is better as a general statement. In some minor cases it will taken longer than appending to a string but these should largely be ignored. I gave you the time taken to require each operation and it took significantly longer without using the StringBuilder.

This article has been dead for over six months. Start a new discussion instead.