Why does "\n" give a new line on Windows?

.net, c#, newline, windows

Solution

`'\n'` is the Line Feed character. Traditionally, it caused the printer to roll the paper up one line. `'\r'` is the Carriage Return character, which traditionally caused the printer head to move to the far left edge of the paper.

On printers and consoles that interpret the characters in this manner, the output of `line1\nline2` would be

line1
     line2

Many consoles (and editors) will interpret '\n' to mean that you want to start a new line and position the cursor at the beginning of that new line. That is what you see here.

You should use Environment.NewLine rather than hard-coding any particular constants.

Problem

The line-break marker on Windows should be `CR+LF` whereas on Unix, it's just `LF`. So when I use something like `Console.Write("line1\nline2");`, why would it work "properly" and give me two lines? I expect this `\n` not to work, and only a combo of `\r\n` would work.

Original source

Related problems