C# StringBuilder ToString() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
StringBuilder

Definition

In C#, StringBuilder.ToString() copies the text in the builder into a new string. It is usually the last step: build the text with Append() and friends, then call ToString() once to get the result. The second overload, ToString(startIndex, length), copies only part of the text, like Substring.

The string you get is a snapshot. Changing the builder later does not change that string, because strings can never change. Many APIs call ToString() for you: Console.WriteLine(sb), "text" + sb, and $"{sb}" all use it. Compare with string.ToString(), which simply returns the same string.

An instance method of StringBuilder (using System.Text;), called as sb.ToString(...). The builder is a mutable buffer that changes in place, unlike an immutable string. It is not thread-safe.

Syntax

Call it with no arguments for the whole text, or with a start position and a length for a part.

C#
StringBuilder sb = new StringBuilder("Hello, World");
string all = sb.ToString();         // "Hello, World"
string part = sb.ToString(7, 5);    // "World"
CallWhat it does
sb.ToString()Returns the whole text as a new string.
sb.ToString(0, 3)Returns the first 3 characters.
sb.ToString(i, sb.Length - i)Returns everything from index i to the end.
Console.WriteLine(sb)Calls ToString() for you before printing.

Each call copies the characters, so calling ToString() inside a loop that also appends can undo the speed benefit of a builder. For the range overload, startIndex and length must not be negative and startIndex + length must not exceed Length; otherwise ArgumentOutOfRangeException is thrown.

Library declarations
C#
public override string ToString();
public string ToString(int startIndex, int length);

Parameters

ParameterDescription
startIndexOptional. The position of the first character to copy (0-based).
lengthOptional, with startIndex. How many characters to copy.

Return Value

Return ValueDescription
stringA new string with the builder's characters (all of them, or the requested part). An empty builder gives "", never null.

C# Examples

Example 1 — Build the Text, Then Convert It Once

Build a phrase in three appends, convert it with one ToString() call, and use normal string members on the result.

C#
using System;
using System.Text;

class Program {
    static void Main() {
        StringBuilder sb = new StringBuilder();
        sb.Append("Build").Append(" then ").Append("convert");

        string result = sb.ToString();
        Console.WriteLine(result);
        Console.WriteLine(result.Length);
        Console.WriteLine(result.ToUpperInvariant());
    }
}

How It Works

  1. Builder for the building. Three chained appends write Build, " then ", and convert into one buffer.
  2. One conversion. ToString() is called once, after the text is complete, and the result is kept in a string variable.
  3. A real string. result.Length is 5 + 6 + 7 = 18, and string-only methods such as ToUpperInvariant() now work on it.
  4. Culture-safe upper case. ToUpperInvariant gives the same letters on every machine, even in cultures with special rules for the letter i.

Example 2 — Read Fields Out by Position

Pull an ID and a name out of a fixed-layout record with the two-argument overload, then ask for a range that is too long.

C#
using System;
using System.Text;

class Program {
    static void Main() {
        StringBuilder sb = new StringBuilder("ID:4821;NAME:Mira");

        string id = sb.ToString(3, 4);
        string name = sb.ToString(13, 4);
        Console.WriteLine(id);
        Console.WriteLine(name);

        try {
            sb.ToString(15, 5);
        } catch (ArgumentOutOfRangeException) {
            Console.WriteLine("range is past the end");
        }
    }
}

How It Works

  1. Count the positions. ID: takes indexes 0 to 2, so the four digits start at index 3. ToString(3, 4) returns 4821.
  2. Second field. NAME: ends at index 12, so the name starts at 13 and is 4 characters long: Mira.
  3. No full copy. Each call copies only the requested characters; the 17-character builder is never turned into one big string first.
  4. Too far. 15 + 5 = 20, but the text has only 17 characters, so the call throws ArgumentOutOfRangeException.

Example 3 — A Snapshot Stays the Same

Take a string from the builder, keep editing the builder, and compare the two. Then let string interpolation call ToString() for you.

C#
using System;
using System.Text;

class Program {
    static void Main() {
        StringBuilder sb = new StringBuilder("v1");
        string snapshot = sb.ToString();

        sb.Replace("1", "2").Append(" (latest)");

        Console.WriteLine(snapshot);
        Console.WriteLine(sb.ToString());
        Console.WriteLine($"Builder says: {sb}");
    }
}

How It Works

  1. Freeze a copy. snapshot holds v1, the text at the moment ToString() ran.
  2. Edit the builder. Replace("1", "2") and Append(" (latest)") change only sb.
  3. Old and new side by side. The snapshot still prints v1, while a fresh ToString() shows v2 (latest).
  4. Automatic call. Inside $"...", the {sb} hole converts the builder with ToString(), so no explicit call is needed.

Frequently Asked Questions

It copies the characters in the builder into a new string and returns it. The overload ToString(startIndex, length) copies only part of the text.
No. The string is a separate copy, and strings never change. Call ToString again to get the latest text.
No. Console.WriteLine(sb), "Text: " + sb, and $"{sb}" call ToString for you. Calling it yourself is fine and makes the intent clear.
No. An empty builder returns an empty string "", never null.
It copies every character, so it costs time proportional to the length. Call it once at the end rather than after every Append.

Back to the method index

See all 13 StringBuilder method guides grouped by what they do.

Method index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful