Open In App

How to create the StringBuilder in C#

StringBuilder() constructor is used to initialize a new instance of the StringBuilder class which will be empty and will have the default initial capacity. StringBuilder is used to represent a mutable string of characters. Mutable means the string which can be changed. So String objects are immutable but StringBuilder is the mutable string type. It will not create a new modified instance of the current string object but do the modifications in the existing string object.

Syntax:



public StringBuilder ();

Example:




// C# Program to illustrate how
// to create a StringBuilder
using System;
using System.Text;
using System.Collections;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // sb is the StringBuilder object
        // StringBuilder() is the constructor
        // used to initializes a new
        // instance of the StringBuilder class
        StringBuilder sb = new StringBuilder();
  
        // Capacity property is used to get
        // maximum number of characters that
        // can be contained in the memory
        // allocated by the current instance
        Console.WriteLine(sb.Capacity);
    }
}

Output:
16

Here 16 is the default capacity.



Note:

Article Tags :
C#