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
Call
What 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 == b
Reference 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
Parameter
Description
sb
The builder to compare with. null gives false.
span
Span overload. The characters to compare with, for example "text".AsSpan().
Return Value
Return Value
Description
bool
true 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);
}
}
Output
True
False
False
How It Works
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.
Case-sensitive.c starts with a capital H. The comparison is ordinal, so a.Equals(c) is False.
No == overload.a == b checks whether the two variables point to one object. They are two separate builders, so it prints False.
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());
}
}
Output
True
False
True
How It Works
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.
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.
Same value, other answer. The text did not change between the calls; only the declared type of the argument did.
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()));
}
}
Output
True
False
True
False
How It Works
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.
Exact letters.apple with a small a differs in one character, so the span comparison prints False.
Ignore case the string way.string.Equals with StringComparison.OrdinalIgnoreCase on the ToString() result treats Apple and apple as equal.
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.