C# StringBuilder ToString Method

Letters of the alphabet: ABC

You want to read about the details of the ToString method on the StringBuilder type in the C# language and .NET Framework. The ToString method internally converts the character buffer of the StringBuilder into a string type, and it does this without allocating memory or copying a lot of data in most cases.

This C# article demonstrates the ToString method on StringBuilder. It examines the performance of ToString.

Example

First, this program demonstrates the ToString method usage on the StringBuilder type and how it can be used to convert a StringBuilder's character buffer into an actual string reference. Because many .NET programs use the string type itself as a common parameter type, converting the StringBuilder's buffer to a string is very useful. The article further shows that the ToString method is efficient because it does not allocate memory in most cases.

Program that uses ToString method on StringBuilder [C#]

using System;
using System.Text;

class Program
{
    static void Main()
    {
	//
	// Begin by declaring a StringBuilder and adding three strings.
	//
	StringBuilder builder = new StringBuilder();
	builder.Append("one ");
	builder.Append("two ");
	builder.Append("three ");
	//
	// Add many strings to the StringBuilder in a loop.
	//
	for (int i = 0; i < 10; i++)
	{
	    builder.Append("x ");
	}
	//
	// Convert the buffer to a string.
	// Often no additional copying is required at this stage.
	//
	string result = builder.ToString();
	Console.WriteLine(result);
    }
}

Output

one two three x x x x x x x x x x
Main method

Description. The compilation unit shown above defines the Main entry point and this method internally creates a StringBuilder and appends 13 strings to its buffer using the Append calls. The character buffer's representation as a string is shown in the text as well.

Calling ToString on StringBuilder. The program at the end of its execution calls the ToString method on the StringBuilder instance. This method internally uses logic and often does not copy the string data an additional time. Instead, ToString sets a null character in the buffer data and transforms the buffer into a string without any allocation occurring. For this reason, calling ToString on most StringBuilder instances is very efficient.

Copying

Question and answer

Let's explore whether the ToString instance override method on the StringBuilder class actually makes another copy of the string data in its buffers when it is called. The ToString method on StringBuilder contains some logic that avoids an additional copy in many cases. An additional copy of the string characters is made when the StringBuilder is being used on multiple threads at the same time.

An additional copy is also made if the memory allocated to the StringBuilder is much larger than required, meaning that the copy will occupy less memory in the long run. In all other cases, the ToString method simply modifies its internal buffer and transforms it into a string for you.

Summary

The C# programming language

We examined the StringBuilder type and its ToString method, which is useful for converting the internal character buffer to a usable string type in the C# language. We noted how the ToString method internally prevents an extra string copy from occurring, which enhances performance and makes StringBuilder even more efficient.

StringBuilder Secrets
.NET