String constant for new line + tab?

I want the C # 4 string constant to represent a new line and tab, as shown below:

internal const string segment = "\r\n\t"; 

I know there is an Environment.Newline that I think I can use like this:

 internal const string segment = Environment.NewLine + "\t"; 

My question is the most efficient way to build a string constant with a new line and tab?

+7
source share
3 answers

If you declare a string as const , as indicated above, there is no difference in efficiency. Any constant will be replaced at compile time and uses an interned string.

Unfortunately, the second option is not a compile-time constant and does not compile. To use it, you need to declare it as:

 internal static readonly string segment = Environment.NewLine + "\t"; 

I personally find this very clear in terms of intent, and that would be my preference, although this would not be a compile-time constant. The additional overhead / loss of efficiency is so incredibly insignificant that I personally chose clear, intentional and understandable code based on the compilation time constant.

Note that using Environment.NewLine also makes sense to be correct if you port this code to Mono, and your goal is to use the line separator of the current platform. In this particular case, the former will be incorrect on platforms other than Windows. If your goal is to specifically include "\r\n\t" and not want a line break for a particular platform, then Environment.NewLine would be an unacceptable choice.

+13
source

const will not work. use static readonly .

 internal static readonly string segment = Environment.NewLine + "\t"; 
+5
source

Pure speed efficiency, then the first will win, especially since the second cannot be made const , and then cannot be compiled.

Nevertheless, the second will be very darn, and I do not care.

More importantly, these are simply not the same thing.

It boils down to "why are you using \ r \ n \ t"?

If you use \ r \ n \ t because you are on Windows, and on Windows newlines are usually \ r \ n, then you should definitely use:

 internal static readonly string segment = Environment.NewLine + "\t"; 

Using another would be wrong and could introduce errors that you won't see for years to come.

If you use \ r \ n because you are working with a specification that says β€œindividual segments with CRLF followed by a tab”, you should definitely use:

 internal const string segment = "\r\n\t"; 

Using another would be wrong.

This is what really bites people. Windows people write HTTP code that is simply used to use a new line of Windows between the headers between the headers and the body. Then it wraps to where the new line \ n is and it breaks because the HTTP credential \ r \ n no matter what system was used. The same thing happens and vice versa.

(Under a strict / permissive principle, it might also be a good idea to assume that other code there will continue to be wrong and take all the different forms of the new line there).

+1
source

All Articles