Home
C#
Environment.NewLine
This page was last reviewed on Jul 19, 2022.
Dot Net Perls
Environment.NewLine. A newline on Windows equals "\r\n." To add newlines in a C# string, there are a couple options—and the best option may depend on your program.
Newline versions. There is a constant from the base class library and the actual string literal. We review these newline options.
Environment
Platform example. We can run "dotnet" programs through the command-line on Linux. With this platform, a NewLine is just 1 character, not 2 like on Windows.
using System; class Program { static void Main() { // Run on Linux. var result = Environment.NewLine.Length; Console.WriteLine(result); } }
1
Concat example. Here we need to create a string with a line break in the middle of it, which will form a 2-line string. We will use the Environment.NewLine constant for this.
Console.WriteLine
Note NewLine is defined by .NET and could vary by platform. But it is unlikely to ever equal a different value.
using System; class Program { static void Main() { // Use string concat to combine two literals with the newline constant. string s = "First line" + Environment.NewLine + "Second line"; Console.WriteLine(s); } }
First line Second line
Literal example. You do not need to use Environment.NewLine as in the first example. You can use the "\r\n" constant directly—this may be a better choice.
Note This constant changes the IL generated. It is effective on Windows platforms and is (in practical aspects) the same.
using System; class Program { static void Main() { // Concat literals. string s = "One line" + "\r\n" + "Another line"; Console.WriteLine(s); } }
One line Another line
Implementation. Environment.NewLine is a constant property in .NET. And it could be tied to the current executing environment or platform.
L_000b: call string [mscorlib]System.Environment::get_NewLine()
.method public hidebysig specialname static string get_NewLine() cil managed { .maxstack 8 L_0000: ldstr "\r\n" L_0005: ret }
Notes, IL. The above IL is generated when you use NewLine. The "get_" part indicates that the value is fetched from a property. This is inlined so has no performance penalty.
Intermediate Language
Property
Next We look into get_NewLine and its implementation. You can see it simply returns the "\r\n" string literal.
String Literal
Note It is possible that a new character set could have another constant as a line break.
A summary. We used the Environment.NewLine constant in the C# language. Some developers find the Environment.NewLine constant has more clarity in their code.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jul 19, 2022 (edit).
Home
Changes
© 2007-2024 Sam Allen.