The StringBuilder Class: An Alternative to Using Strings

The Microsoft docs page does a great job of describing the difference between the StringBuilder and String classes. I decided to write about it anyway because it’s one of those things most beginners aren’t even aware of. I think it’s useful to give beginners a better understand of what strings actually are too.

The essential difference between the two classes comes down to the concept of mutability, the ability to change. Where this is so confusing is that C# strings technically are immutable. This may come as a surprise given the way you’ve always used strings. You use the + operator all the time to concatenate strings and get an updated result for example. But things are happening under the hood that aren’t being shared with you. In general I’ve found these kinds of happenings very frustrating as a newcomer to languages. I understand why languages do things that way, but the frustration came from a place of- “well how was I supposed to know that??” This article is your chance to know that. From the docs:

Here’s your proof that strings are immutable. When you think you’re modifying them, you’re actually getting a new string that contains your modification applied to the previous version of the string. And this isn’t all bad. Nobody is saying not to use string any longer. But you should be aware that string may not always be the best option. The docs provide this general recommendation on how to choose between the two options:

As usual you should test the performance between two options, on the hardware that your end users will be using, to be sure which is more performant. This extends beyond the point of which string type to use. And this formal testing isn’t always necessary. If your strings aren’t being used much it’s not worth the trouble. If you’re changing string contents millions of times, maybe it is.

StringBuilder on the other hand is mutable. And don’t be intimidated that StringBuilder is a class that’s new to you. It really isn’t hard to understand and use. Instead of using the + operator to concatenate text, the StringBuilder class has various “Append” methods. You get your typical Insert, Remove, and Replace functionality as well. There’s even a handy “ToString” method that lets you get back to a regular string for when you’re done with all your modifications and need to provide the result somewhere else in your code that expects a string. Another notable difference with StringBuilder is that you create an instance of the class with the “new” keyword to start using it. Here are some usage examples to highlight what I’m saying:

If you’re already comfortable with strings, you’ll pick up StringBuilder in no time. It’s pretty easy to pick up otherwise too. You’ll notice in the coding challenge videos I do that I sometimes use StringBuilder, just to work it in and raise awareness. It’s often not relevant for the typically small challenges we do at the beginner levels. But my goal with the videos is to teach students about what is available. I usually call this out in the videos, but don’t overthink things when you see me using StringBuilder at times. You know you can drop your questions in the comments whenever you have them too.

So I challenge you to work StringBuilder into your repertoire, at least in practice projects. Make sure you have this tool in your belt.

Previous
Previous

Why Do We Bother With All These “public” and “private” Keywords?

Next
Next

Know your LINQ- Enumerable Select Method