开发者

StringBuilder.Append Question

开发者 https://www.devze.com 2022-12-15 11:45 出处:网络
I would like to know the difference (if there is any) between : StringBuilder sb = new StringBuilder();

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 :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号