In this article, we’re going to talk about the StringBuilder class, and explore options to check if it is empty, directly or indirectly.

To download the source code for this article, you can visit our GitHub repository.

So, let’s start.

What Is a StringBuilder Class?

StringBuilder is a string-like object whose value is a mutable sequence of characters. It is a class designed to make string manipulation as easy and as fast as possible. 

Support Code Maze on Patreon to get rid of ads and get the best discounts on our products!
Become a patron at Patreon!

It is recommended to use this class when we make heavy use of string concatenation, appending, inserting, removing, etc. String manipulation is expensive if we use String class thanks to how the String class operates in the .NET framework.

Let’s say that we decided to use the StringBuilder class and want to check if a StringBuilder object is empty.

We can do this in two ways, directly or indirectly.

Check if StringBuilder Is Empty Using the Length Property

The StringBuilder class has a property named Length that shows how many Char objects it holds.

So, let’s use the Length  property:

var filledStringBuilder = new StringBuilder("example string value");
if(filledStringBuilder.Length == 0)
{
    Console.WriteLine("StringBuilder is empty!");
}
else
{
    Console.WriteLine($"StringBuilder is not empty and lenght is {filledStringBuilder.Length} characters");
}

 If Length is zero that means the instance contains no characters.

Check if StringBuilder Is Empty Using String Class Methods

To check if StringBuilder is empty, we can use the String class methods as well. But before we do that, we have to convert the StringBuilder instance to a String instance:

var emptyStringBuilder = new StringBuilder();

var emptyString = emptyStringBuilder.ToString();

Then, we can use other String class methods to check emptiness after converting StringBuilder to a String class:

var emptyStringBuilder = new StringBuilder();

var converted2String = emptyStringBuilder.ToString();

if (string.IsNullOrEmpty(converted2String))
{
    Console.WriteLine("emptyStringBuilder is empty!");
}

Or, we can compare directly:

if (emptyStringBuilder.ToString() == "")
{
    Console.WriteLine("emptyStringBuilder is empty!");
}

Lastly, let’s use the String.Empty property to make the comparison:

if (emptyStringBuilder.ToString() == String.Empty)
{
    Console.WriteLine("emptyStringBuilder is empty!");
}

Now if we start our application and test this implementation, we can see that everything works as expected:

StringBuilder is not empty and lenght is 20 characters
emptyStringBuilder is empty!
emptyStringBuilder is empty!
emptyStringBuilder is empty!

One more thing to mention here. The String class in C# and .NET (Core) are immutable. That means, even if you use the same string variable, after every operation, a new string object is created. This causes too many memory allocations and it is a costly operation for resources. The StringBuilder class does not create a new object for every operation so it costs fewer resources to use it.

Conclusion

In this article, we have learned how to check if a StringBuilder object is empty. We have provided several examples in both a direct way by using the Length property and an indirect way with the String class. 

Liked it? Take a second to support Code Maze on Patreon and get the ad free reading experience!
Become a patron at Patreon!