StringBuilder.Append Question
I would like to know the difference (if there is any) between :
StringBuilder sb = new StringBuilder();
sb.Append("xxx")开发者_StackOverflow社区;
sb.Append("yyy");
sb.Append("zzz");
And :
StringBuilder sb = new StringBuilder();
sb.Append("xxx")
.Append("yyy")
.Append("zzz");
There's no difference, the latter case is called 'Fluent Syntax'.
There is no functional difference. In your first case, you're appending strings to the same StringBuilder
in the same order in three statements. In your second case, you're appending the same strings to the same StringBuilder
in one statement.
There will be no notable performance difference and absolutely no observable difference. You may choose one or the other stylistically.
The IL surely will look differently between the two, and just from first looks, the chained version may be a few nanoseconds faster (I just like saying nanoseconds.), but it's nothing to consider refactoring your code over. If it looks cleaner one way, I'd take that path.
It always bugged me, how StringBuilder.Append
would always return itself. It causes one to think that the StringBuilder is an immutable structure, when it most definitely is not. It is also especially annoying in F#, where you have to explicitly ignore what it returns. But whatcha gonna do?
There is no difference between them. But you can use second one for less code line :)
精彩评论