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
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.
If it is one character, better use sb.Append(' ');
sb.Append(' ');
You probably think that there is an alternative, for example string.Empty for "" . But there is no such thing, so using " " is fine.
string.Empty
""
" "
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; }
You might be better off defining const char seperator = ' ' . Then using sb.Append(seperator)
const char seperator = ' '
sb.Append(seperator)
This will make the code more convenient to maintain if you later decide to use (for example) comma separation.
Source: https://habr.com/ru/post/1301549/More articles:How to update a running asp.net application? - performanceTomcat web application thread dump - javaDefinition could not be found compilation error ClassReference in CSS file to Swf file - flexHow to manage an instance of Singleton? - c #What to do with exceptions thrown by SwingUtilities.invokeAndWait - java-isysroot or SDKROOT - cWhy is Firefox requesting a dummy URL from IMG src in JavaScript code? - javascriptPlay Javascript with 360 ° VR View from iPad Gallery - javascriptHow to override the style of hidden hyperlinks? - htmlHow to achieve QT-like syntax for signal connections using Boost :: Signal - qtAll Articles