C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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
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.Write(s);
Console.Read();
}
}
Output
First line
Second line
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
using System;
class Program
{
static void Main()
{
//
// Use "\r\n" string in string concat.
//
string s = "One line" +
"\r\n" +
"Another line";
Console.Write(s);
Console.Read();
}
}
Output
One line
Another line
Call instruction
L_000b: call string [mscorlib]System.Environment::get_NewLine()
Implementation of get_NewLine
.method public hidebysig specialname static string get_NewLine()
cil managed
{
.maxstack 8
L_0000: ldstr "\r\n"
L_0005: ret
}
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.
String LiteralNote: It is possible that a new character set could have another constant as a line break.