How To Create Dynamic Strings In C#

After reading this article, you will learn the points, as shown below.

  1. Introduction
  2. How to concatenate the strings
  3. When to use String Builder class
  4. Methods of String Builder class
  5. Examples
  6. Interview Question


As we all know that the strings are immutable, because after a string is stored in the memory, the memory, which is allocated for the string, cannot change.

If the string changed, a new memory location is needed to store the changed string. In these circumstances, we use StringBuilder class with a string type.

For example, let’s look at the example given below for clear understanding 

  1. Int amount=26;  
  2. String s1=”My age is ”;  
  3. S1=string.concat (s1,amount.ToString());   

In the code given above, s1 is created and it changed, so old and new s1 will be stored temporarily in memory. The old s1 will be cleared from the memory by a garbage collection process. If our Application frequently changes the string, then obviously we may hold large memory in use, while waiting for the next garbage collection to clean up the memory.

Concatenating strings

The string.Concat method creates a new string and concatenates with the result of the ToString method and then stores the result in the new memory location, which is then linked to s.

This means, we have two strings when we need only one. Also, in real time scenario, we may work with multiple strings. Let’s say that we had a situation to concatenate the strings in a loop. This situation can be both a performance and memory problem

Thus, to overcome this issue, we need to implement String.Builder class in System.Text namespace.

When to use StringBuilder Class

If we concatenate the string in for loop, where a large number of strings are stored in the memory, then it is recommended to use StringBuilder Class.

StringBuilder also acts like the collection classes. It allocates an initial value of 16 characters and if your string becomes larger than this, it automatically grows to accommodate the string size.

We can implement string builder, as shown below. 

  1. int amount =21;  
  2. StringBuilder sb= new StringBuilder("My Balance is ");  
  3. sb.append(amount);   

The code given above contains only one string, which is referenced by sb.

Methods of StringBuilder class

There are 5 important methods in stringbuilder class, as shown below.

  1. Append
  2. AppendFormat
  3. Insert
  4. Remove
  5. Replace


This method places an item at the end of current StringBuilder.


This method specifies a format for the object.


Places the object at the specific index.


As the name indicates, it removes the characters.


As the name indicates, it replaces the characters.

Examples of Each method

Let us create an object, as shown below.

  1. StringBuilder sb= new StringBuilder(“ABCD”);  


  1. Way of Appending

  2. Output


  1. Way to apply appendformat
    s.AppendFormat (“{0:n}”,1100);

  2. Output


  1. Way to insert an object

  2. Output


  1. Way to Remove the object

  2. Output


  1. Way to Replace an object

  2. Output

Interview Question

After executing the code given below, how can you preserve computer memory?

  1. for(int i=0; i<1000;i++)  
  2. {  
  3.     s=s.Concat(s,i.ToString());  
  4. }   


  1. StringBuilder= new StringBuilder();  
  2. for(int i=0; i<1000;i++)  
  3. {  
  4.     s.Append(i);  
  5. }   

Thanks for reading this article. Sharing is caring.

Up Next
    Ebook Download
    View all
    View all