C# | Check if two StringBuilder objects are Equal
Last Updated :
11 Jul, 2025
Improve
StringBuilder.Equals Method is used to check whether this instance is equal to a specified object.
Syntax: public bool Equals (System.Text.StringBuilder sb); Here, sb is an object to compare with this instance, or null. Return Value: It will return true if this instance and sb have an equal string, Capacity, and MaxCapacity values; otherwise, false.
Example 1:
// C# program to if a StringBuilder object
// is equal to another StringBuilder object
using System;
using System.Text;
class Geeks {
// Main Method
public static void Main(String[] args)
{
// Create a StringBuilder object
// with a String passed as parameter
StringBuilder st1 = new StringBuilder("GeeksforGeeks");
// Checking whether st1 is
// equal to itself or not
Console.WriteLine(st1.Equals(st1));
}
}
Output:
True
Example 2:
// C# program to if a StringBuilder object
// is equal to another StringBuilder object
using System;
using System.Text;
class Geeks {
// Main Method
public static void Main(String[] args)
{
// Create a StringBuilder object
// with a String passed as parameter
StringBuilder st1 = new StringBuilder("GeeksforGeeks");
// Create a StringBuilder object
// with a String passed as parameter
StringBuilder st2 = new StringBuilder("GFG");
// Checking whether st1 is
// equal to st2 or not
Console.WriteLine(st1.Equals(st2));
// Create a StringBuilder object
// with a String passed as parameter
StringBuilder st3 = new StringBuilder();
// Assigning st2 to st3
st3 = st2;
// Checking whether st3 is
// equal to st2 or not
Console.WriteLine(st3.Equals(st2));
}
}
Output:
False True
Reference: