C# Reference ยท Strings and Text
Lines and line endings in C#
Reference for line endings in C#: Environment.NewLine, \n versus \r\n, splitting text into lines, StringReader, EnumerateLines and WriteLine behavior.
Text files mark the end of a line with a control character: \n (line feed, used by Linux and macOS), \r\n (carriage return plus line feed, used by Windows) or, rarely, a lone \r. Environment.NewLine is the sequence for the platform your program runs on, and Console.WriteLine and StreamWriter.WriteLine use it for you.
The trouble starts when text crosses systems: a file saved on Windows read on Linux, HTTP and email using \r\n always, or a string literal containing \n compared to input that has \r\n. When reading, handle all three forms; when writing, choose deliberately.
string text = "one\r\ntwo\nthree"; foreach (string line in text.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None)) { Console.WriteLine(line); }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
public static string Environment.NewLine { get; } public string string.ReplaceLineEndings() public string string.ReplaceLineEndings(string replacementText) public virtual string? TextReader.ReadLine() public static SpanLineEnumerator MemoryExtensions.EnumerateLines(this ReadOnlySpan<char> span) public virtual string TextWriter.NewLine { get; set; }
| Name | Description |
|---|---|
replacementText |
The sequence that replaces every line break. The no-argument overload uses Environment.NewLine. |
Returns: NewLine is "\r\n" on Windows and "\n" on Linux and macOS. ReadLine returns each line without its terminator, or null at the end of the input.
Errors / undefined behavior: none for the methods above. ReplaceLineEndings also treats the Unicode separators NEL (U+0085), LS (U+2028) and PS (U+2029) as line breaks, which can surprise you.
Usage notes
- Read tolerantly, write deliberately.
StringReader,StreamReader.ReadLine,File.ReadLinesandEnumerateLines()accept every common ending.Split('\n')does not: it leaves a trailing\r. - Protocols fix the ending. HTTP headers, SMTP and many text protocols require
\r\nregardless of the platform. Write"\r\n"explicitly there instead ofEnvironment.NewLine. - Tests that compare text. A literal with
\nfails againstConsole.WriteLineoutput on Windows. Normalize withReplaceLineEndings("\n")on both sides. - Source file endings leak into raw strings. A multi-line raw or verbatim string contains whatever line endings your source file has. Normalize if the value is compared or hashed.
- Trailing line break.
"a\nb\n"yields two lines withReadLine, butSplit('\n')yields three, the last empty. - Changing the writer's ending. Set
TextWriter.NewLineonce (as in the example) rather than appending"\n"by hand;WriteLinethen uses it everywhere. Environment.NewLineis not "the file's ending". It describes the machine running the code. Data files should pick a fixed convention.