Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Environment.NewLine = "\r\n" when "\n" in a string literal functions the same?

Tags:

c#

.net

In C# if you do something like string newLine = Environment.NewLine; and inspect the value of newLine you'll find that it is "\r\n". However, if I do something like;

  string[] test = new string[] { "one", "two", "three" };

  Console.WriteLine("With plain slash n:");
  Console.Write(String.Join("\n", test));
  Console.WriteLine("\nWith Environment.NewLine:");
  Console.Write(String.Join(Environment.NewLine, test));

The output is functionally the same.

Why is Environment.NewLine set to \r\n instead of just \n? Doesn't that just make it more difficult to convert from string to character array? Also I read something which led me to believe some input fields don't count Environment.NewLine as two characters which could cause problems if your backing fields are restricted to the length of the input field. Are there any positive reasons for it? Is it just legacy cruft at this point?

like image 650
evanmcdonnal Avatar asked Dec 16 '13 22:12

evanmcdonnal


People also ask

What is environment NewLine?

The Enviornment. NewLine in C# is used to add newline. To set a new line in between words − str = "This is demo text!" + Environment.NewLine + "This is demo text on next line!"; The following is the code −

What is \r in a string?

Just (invisible) entries in a string. \r moves cursor to the beginning of the line. \n goes one line down.

How do you add a new line to a string literal?

Adding Newline Characters in a String In Windows, a new line is denoted using “\r\n”, sometimes called a Carriage Return and Line Feed, or CRLF. Adding a new line in Java is as simple as including “\n” , “\r”, or “\r\n” at the end of our string.


2 Answers

The point of Environment.NewLine is to return the newline character for the platform. Presumably for implementations of the .NET framework atop Linux or Unix, it would return just \n.

like image 170
Jason Malinowski Avatar answered Sep 30 '22 14:09

Jason Malinowski


"New line" in windows/dos is, by convention, CR+LF ("\r\n", "\x0D\x0A"). In the *nix world, "new line" is, by convention, a single LF character ("\n","\0A"). In the pre-OS X Macintosh world, "new line" is, again by convention, a single LF character ("\r","\x0D"). Toss IBM mainframes into the mix and its worse: EBCDIC control character set comprises CR, LF and a specific NL control character.

The reason for this is historical and has to do with the fact that ASCII doesn't have an actual NL (new line) control character: it just has CR (carriage return) and LF (line feed). The various OSes were developed with different printing terminals, and their interpretation of the meaning of these control characters, on which the various operating systems were developed and you have your newline schism.

The CLR I/O system tries to follow Postel's Law ("Be generous in what you accept and strict in what you emit") and so is agnostic on input WRT new line conventions. Consequently, the standard CLR TextReader implementations accept any of CR+LF, CR or LF as constituting a new line indicator. However, on output, WriteLine(), by default, appends the value of `Environment.NewLine'.

like image 36
Nicholas Carey Avatar answered Sep 30 '22 12:09

Nicholas Carey