which is faster
so in c# there is something called StringBuilder i just figured it days ago
so my question which is is better to use
string extra = "Hello " + " World " + " extra stuff";
or to use StringBuilder for same purpose when I searched in Google it says that stringbuilder is much faster so just wanted to ask and make sure.
5 Replies
tldr: string builder is probably the fastest, especially for longer combinations.
but for something trivial like
var greeting = "Hello " + name + ", welcome to my program!";
then string interpolation is your best bet
being var greeting = $"Hello {name}, welcome to my program!";
yeah, go for a builderthanks
These days, string interpolation is almost always going to be faster than a
StringBuilder
for shorter sequences, yes. for longer sequences it will use a stringbuilder (but perhaps better than you did)
oh he deleted the screenshot.
It was a ~30-40 different things being concatted to build some kind of package payload
Yep exactly, and it'll use
StringBuilder
in a way which doesn't require creating intermediate strings