Javatpoint Logo
Javatpoint Logo

StringBuilder.EnsureCapacity() Method in C#

In C# programming, the StringBuilder class is a tool for handling and manipulating strings. It is useful for dynamic string concatenation or frequent modifications to strings. The EnsureCapacity() method is designed to optimize the performance by pre-allocating memory space.

Unlike traditional string manipulation using the concatenation operator '+'. The StringBuilder offers a more efficient approach for building and modifying strings. It maintains an internal buffer to store the characters of the string. This buffer is dynamically resized as needed, allowing more efficient memory management than creating new string objects for each concatenation operation.

StringBuilder dynamically adjusts its capacity as strings grow. There are scenarios where it makes sense to proactively allocate a certain amount of memory space, such as when the EnsureCapacity() is used. The purpose of EnsureCapacity() is to set the capacity of the internal buffer to a specified minimum size, ensuring that it can accommodate a given number of characters without reallocation.

The EnsureCapacity() checks whether the current capacity of the StringBuilder is less than the specified minimum capacity. If the capacity is less, then the method will increase the capacity. No changes are made if the current capacity is greater than or equal to the specified capacity.

Syntax:

It has the following syntax:

This method will take one parameter capacity. It takes an integer, which represents the minimum capacity to ensure. This method will return an integer value representing the new capacity of the StringBuilder after ensuring that it meets or exceeds the specified capacity.

Example:

Let us take a C# Program to illustrate the EnsureCapacity() method.

Output:

StringBuilder.EnsureCapacity() Method in C#

Explanation:

This C# program show the importance of the EnsureCapacity() method. First, a StringBuilder is created which represent a dynamic string that used as a grocery list. First, a StringBuilder is created, which represents a dynamic string that is used as a grocery list. After that, the initial capacity for the StringBuilder is set at 20. Using the EnsureCapacity() method to ensure that StringBuilder that has at least the specified initial capacity. Create an AddItemToGriceryList method to add the strings to the list. This method appends the formatted item to the grocery list. The variable averageItemLength represents the average length of the item in the list. Again the EnsureCapacity() method ensures the capacity of the StringBuilder is sufficient to accommodate the next item. After that, the final grocery list is displayed along with its capacity.

Example 2:

Let us take another example program to demonstrate the StringBuilder.EnsureCapacity() method in C#.

Output:

StringBuilder.EnsureCapacity() Method in C#

Explanation:

This C# program utilizes a StringBuilder to simulate a dynamic log, emphasizing memory optimization through the EnsureCapacity method. The AddLogMessage helper function appends formatted log entries with timestamps and message levels, dynamically adjusting capacity based on the average message length. The enum LogLevel categorizes messages into Info, Warning, Error, and Debug levels. The program first displays the initial capacity, simulates adding log messages, and dynamically adjusts capacity as needed. The use of EnsureCapacity() method prevents unnecessary memory reallocations, ensuring efficient string manipulation. It exemplifies the strategic application of StringBuilder in scenarios where the string length is dynamic.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA