C# StringBuilder Equals() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
StringBuilder

Definition

In C#, StringBuilder.Equals(other) returns true when two builders hold the same characters in the same order. The comparison is ordinal: exact characters, case-sensitive, no culture rules. In .NET Core 3.0 and later that is all it checks. In .NET Framework it also checks that Capacity and MaxCapacity are equal, so two builders with the same text can still be "not equal" there.

Watch out for two look-alikes. == is not overloaded for StringBuilder, so a == b only asks "same object?". Equals(object) is inherited from Object, so a builder typed as object is also compared by reference. When in doubt, compare a.ToString() == b.ToString(), as with string.Equals.

An instance method of StringBuilder (using System.Text;), called as sb.Equals(...). The builder is a mutable buffer that changes in place, unlike an immutable string. It is not thread-safe. Equals(StringBuilder) is in every .NET version; Equals(ReadOnlySpan<char>) was added in .NET Core 2.1 and later.

Syntax

Pass another builder, or a span of characters to compare against.

C#
StringBuilder a = new StringBuilder("hi");
StringBuilder b = new StringBuilder("hi");
bool sameText = a.Equals(b);              // True
bool sameObject = a == b;                 // False (different objects)
bool vsSpan = a.Equals("hi".AsSpan());    // True
CallWhat it does
a.Equals(b)Same characters? (.NET Framework: also same capacity.)
a.Equals("text".AsSpan())Compares the builder with any characters, such as a string, without calling ToString().
a == bReference check: true only if both variables point to one builder.
a.ToString() == b.ToString()String comparison of the text; the same on every .NET version.

Equals(null) returns false. Pass strings through AsSpan() so the span overload is clearly chosen; a plain string may end up at the inherited Equals(object), which compares references. For case-insensitive checks, compare ToString() results with StringComparison.OrdinalIgnoreCase.

Library declarations
C#
public bool Equals(StringBuilder? sb);
public bool Equals(ReadOnlySpan<char> span);   // .NET Core 2.1+

// Inherited from Object (reference equality):
public virtual bool Equals(object? obj);

Parameters

ParameterDescription
sbThe builder to compare with. null gives false.
spanSpan overload. The characters to compare with, for example "text".AsSpan().

Return Value

Return ValueDescription
booltrue if the characters match (and, in .NET Framework, the capacities too); otherwise false.

C# Examples

Example 1 — Equals Compares Text, == Compares Objects

Create two builders with the same word and one with a capital letter, then compare them with Equals and ==.

C#
using System;
using System.Text;

class Program {
    static void Main() {
        StringBuilder a = new StringBuilder("hello");
        StringBuilder b = new StringBuilder("hello");
        StringBuilder c = new StringBuilder("Hello");

        Console.WriteLine(a.Equals(b));
        Console.WriteLine(a.Equals(c));
        Console.WriteLine(a == b);
    }
}

How It Works

  1. Same characters. a and b both hold hello and both have the default capacity of 16, so a.Equals(b) is True on every .NET version.
  2. Case-sensitive. c starts with a capital H. The comparison is ordinal, so a.Equals(c) is False.
  3. No == overload. a == b checks whether the two variables point to one object. They are two separate builders, so it prints False.
  4. Rule of thumb. Use Equals (or compare ToString() results) for text, and keep == for "is it the very same builder?".

Example 2 — Builders Built Differently, and the object Trap

Build the same text in two different ways, compare them, then pass one of them as object and see the inherited Equals at work.

C#
using System;
using System.Text;

class Program {
    static void Main() {
        StringBuilder a = new StringBuilder();
        a.Append("ab").Append("cd");
        StringBuilder b = new StringBuilder("abcd");

        Console.WriteLine(a.Equals(b));

        object boxed = b;
        Console.WriteLine(a.Equals(boxed));
        Console.WriteLine(a.ToString() == b.ToString());
    }
}

How It Works

  1. Different history, same text. a was filled by two appends and b by the constructor. Both hold abcd with capacity 16, so Equals is True.
  2. The object trap. With boxed typed as object, the compiler picks Equals(object) inherited from Object. That checks references, and a and b are different objects.
  3. Same value, other answer. The text did not change between the calls; only the declared type of the argument did.
  4. The safe check. Comparing the two ToString() results uses string equality, which always looks only at the characters.

Example 3 — Compare a Builder With Text Using a Span

Compare a builder with string literals through AsSpan(), do a case-insensitive check, and compare again after an append.

C#
using System;
using System.Text;

class Program {
    static void Main() {
        StringBuilder sb = new StringBuilder("Apple");

        Console.WriteLine(sb.Equals("Apple".AsSpan()));
        Console.WriteLine(sb.Equals("apple".AsSpan()));
        Console.WriteLine(string.Equals(sb.ToString(), "apple",
            StringComparison.OrdinalIgnoreCase));

        sb.Append('!');
        Console.WriteLine(sb.Equals("Apple".AsSpan()));
    }
}

How It Works

  1. Span overload. "Apple".AsSpan() turns the literal into a ReadOnlySpan<char>, so Equals(ReadOnlySpan<char>) runs and compares only the characters. Capacity plays no part here.
  2. Exact letters. apple with a small a differs in one character, so the span comparison prints False.
  3. Ignore case the string way. string.Equals with StringComparison.OrdinalIgnoreCase on the ToString() result treats Apple and apple as equal.
  4. Length counts. After Append('!') the builder holds 6 characters, so it no longer equals the 5-character Apple.

Frequently Asked Questions

Equals(StringBuilder) returns true when both builders hold the same characters. In .NET Framework it also requires the same Capacity and MaxCapacity; in .NET Core 3.0 and later only the characters count.
StringBuilder does not overload the == operator, so == checks whether both variables point to the same object. Use Equals or compare ToString() results instead.
Yes. It uses ordinal comparison. For a case-insensitive check, compare sb.ToString() with StringComparison.OrdinalIgnoreCase.
Yes, with sb.Equals("text".AsSpan()) in .NET Core 2.1 and later, or with sb.ToString() == "text" in any version.
Then the inherited Object.Equals(object) is called, which compares references. Keep the argument typed as StringBuilder to use the text comparison.

Next: GetChunks()

Walk the builder's internal pieces of text as ReadOnlyMemorychunks, without making one big string.

GetChunks() tutorial →

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.

14 people found this page helpful