
You have to add newline characters to your C# strings. There are a couple options in the C# programming language, including a constant from the base class library and the actual literal, and you want to choose the best way. We see the different newline constants and examples of their use, first looking at the constant itself and then the internal implementation.
Key points: Environment.NewLine is a string literal. It has the value of "\r\n" in Windows. It can make the intent of your code clearer.
Here we need to create a string with a line break in the middle of it, which will form a two-line string. We will use the Environment.NewLine constant for this, which is defined by the .NET runtime and could vary by platform.
Program that uses Environment.NewLine [C#]
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 lineHere we see that you can simply use a string literal to add a line break. You don't need to use Environment.NewLine as in the first example; you can use the "\r\n" constant directly, and this can actually be a better choice. It changes the MSIL generated, and is effective on Windows platforms.
Program that uses newline constant [C#]
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
As we noted before, Environment.NewLine is simply a constant property in .NET, and it could be tied to the current executing environment or platform. However, almost all C# program deployments use Windows, with the exception of Mono on Linux.
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
}Description. The above IL is generated when you call Environment.NewLine. The "get_" part simply indicates that the value is fetched from a property. This is inlined so has no performance penalty.
Intermediate Language Property PerformanceExamining the internals. Next, we look into get_NewLine and it is implemented with this IL. You can see it simply returns the "\r\n" string literal. You can find more information on the usage and implementation of string literals in the C# programming language on this website.
String Literal
One possibility with new lines is that Environment.NewLine could result in different constants being returned, such as "\n" on UNIX platforms with Mono. Theoretically, a new character set could have another constant as a line break.
In this tutorial, we saw ways you can use the Environment.NewLine constant in the C# programming language. Some developers find the Environment.NewLine constant has more clarity in their code. However, I think that no one will ever forget what \r\n or \n mean, so the clarity is no better.
String Type