Monday, November 1, 2021

C# String vs StringBuilder



String and StringBuilder are two important classes in C sharp programming which are used for manipulation of strings. It is very important for a programmer to understand the difference between these two, as strings are used extensively in programs and string manipulation within the programs is very common.
  • String class belongs to System namespace.
  • StringBuilder belongs to System.Text namespace.
The String class should be used in situations when the value of the string within the program is usually constant, immutable, meaning that it does not require much manipulation. On the contrary, the StringBuilder class should be used when the string is to be manipulated repeatedly in the program. 

The reason behind this is that when the concatenation of a string occurs with another string, a new string object is created within the heap. If this process is repeated again and again, many string objects will be created within the heap. Thus a lot of memory will be used up to allocate memory within the heap. In other words, the memory usage will slow down the speed of the program and will adversely affect the performance of the program. StringBuilder class should be used if you have to do string manipulation repeatedly in your program. The property of StringBuilder class is that when string concatenation is performed again and again, new objects of StringBuilder are not created in the heap. On the contrary, the capacity of the StringBuilder object increases, only its memory allocation changes.

An object of the StringBuilder class has an initial capacity of its own. Initially this capacity is of 16 characters i.e. 2 bytes. In other words, initially the StringBuilder object holds 16 characters in its memory, but if the size of the string exceeds 16 characters, the capacity of the StringBuilder object is doubled. That is to say, it increases from 16 characters to 32 characters i.e. 4 bytes. As the size of the string increases, the capacity of the StringBuilder object also increases, but only one and the same object of the StringBuilder remains in the heap.

On comparing StringBuilder and String class, it is found that the performance of StringBuilder class is better than that of String class. We can also understand this fact with the help of the following example.

using System;
using System.Diagnostics;
using System.Text;

namespace StringVsStringBuilder
{
    class Program
    {
        static void Main(string[] args)
        {
            string str1 = "";
            string str2 = "";
            Stopwatch sw1 = new Stopwatch();
            sw1.Start();
            for (int i = 0; i < 100000; i++)
            {
                str1 = str1 + i;
            }
            sw1.Stop();
            Console.WriteLine("\nElapsed watch for String : {0}", sw1.ElapsedMilliseconds);
            Stopwatch sw2 = new Stopwatch();
            sw2.Start();
            StringBuilder sb = new StringBuilder(str2);
            for (int j = 0; j < 100000; j++)
            {
            sb.Append(j);
            }
            sw2.Stop();
            Console.WriteLine("Elapsed watch for StringBuilder: {0}", sw2.ElapsedMilliseconds);
            Console.ReadLine();
        }
    }
}

No comments:

Post a Comment

Hot Topics