.Net: better equiv for "" (space)?

I use the line builder as part of the logging process. my seperator character I use is ". How can I get this char in a more efficient way than just" ".

For instance:

sb.Append(" "); 

Or is this an acceptable way to do this?

Thanks in advance

+4
source share
5 answers

If you fear creating a new string object each time, stop worrying. The compiler optimizes it to use the same string object for every call.

+7
source

If it is one character, better use sb.Append(' ');

+6
source

You probably think that there is an alternative, for example string.Empty for "" . But there is no such thing, so using " " is fine.

+3
source

It's fine.

However, you can slightly increase the level of abstraction:

 public static StringBuilder AppendWithSeparator(this StringBuilder sb, string value) { sb.Append(value); sb.Append(" "); return sb; } 
+2
source

You might be better off defining const char seperator = ' ' . Then using sb.Append(seperator)

This will make the code more convenient to maintain if you later decide to use (for example) comma separation.

+2
source

Source: https://habr.com/ru/post/1301549/


All Articles