Just to add more value to Jon Skeet's answer, it would be worth to mention that this is a fluent API design.
It has its pros and cons, but as Jon Skeet has already pointed out, not only with StringBuilder
but in many other classes and frameworks, being able to fluently configure or execute things is very interesting.
Maybe the OP has never seen list.Where(x => ...).OrderBy(x => ...).Select(x => ...).ToList()
. Or Castle Windsor's fluent configuration API: Component.For<X>().ImplementedBy<Y>().LifeStyleSingleton()
.
That's why StringBuilder
returns the instance from which Append
was called: to being able to call other methods as a chain which, sometimes, seems to be easier to understand than many separate sentences.
About the cons
@Jason asked me for the cons in some comment:
Matias, What are the cons?
Taken from my own experience, I believe that there're few cons, but one of most important ones might be that a fluent chain can be harder to debug since you can't go step by step during an interactive debugging session (actually you can step into and step out... but it's less comfortable than going step by step...).
Excepting that con I find fluent design a good way of implementing auto-documented code since sometimes it's just like reading a natural language sentence and keeps things simple (to those that have already got used with fluent design...)