C# Environment.NewLineUse the Environment.NewLine property from the System namespace. NewLine equals \r\n.
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.
There is a constant from the base class library and the actual string literal. We review these newline options.
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.
Note NewLine is defined by the .NET Framework and could vary by platform. But it is unlikely to ever equal a different value.
C# program that uses Environment.NewLine
static void Main()
// Use string concat to combine two literals
// with the newline constant.
string s = "First line"
You don't 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.
C# program that uses newline constant
static void Main()
// Use "\r\n" string in string concat.
string s = "One 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()
Implementation of get_NewLine
.method public hidebysig specialname static string get_NewLine()
L_0000: ldstr "\r\n"
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.
Next We look into get_NewLine and its implementation. You can see it simply returns the "\r\n" string literal.
Tip You can find more information on the usage and implementation of string literals in the C# language on this website.
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.
© 2007-2021 sam allen. send bug reports to firstname.lastname@example.org.