C# | Creating StringBuilder having specified capacity

StringBuilder(Int32) constructor is used to initialize a new instance of the StringBuilder class which will be empty and will have the specified 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 (int capacity);

Here, capacity is the starting size of the StringBuilder instance. If the number of characters which is to be stored becomes greater than the specified capacity then the StringBuilder object will dynamically allocate the memory to store them.

Exception: This will give ArgumentOutOfRangeException if the capacity is less than zero.

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to illustrate how
// to create a StringBuilder having
// specified initial capacity
using System;
using System.Text;
using System.Collections;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // sb is the StringBuilder object
        // StringBuilder(10) is the constructor
        // used to initializes a new
        // instance of the StringBuilder class
        // having 10 as capacity
        StringBuilder sb = new StringBuilder(10);
  
        Console.Write("Capacity of StringBuilder: ");
  
        // using capacity property
        Console.WriteLine(sb.Capacity);
    }
}

chevron_right


Output:

Capacity of StringBuilder: 10

Example 2: For ArgumentOutOfRangeException

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to illustrate how
// to create a StringBuilder having
// specified initial capacity
using System;
using System.Text;
using System.Collections;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // sb is the StringBuilder object
        // taking capacity less than zero
        StringBuilder sb = new StringBuilder(-4);
  
        // using capacity property
        Console.WriteLine(sb.Capacity);
    }
}

chevron_right


Runtime Error:

Unhandled Exception:
System.ArgumentOutOfRangeException: ‘capacity’ must be greater than zero.
Parameter name: capacity

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.