C# Environment.NewLine

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.

Environment.NewLine is a string literal.        
... It has the value of "\r\n" in Windows.      
... It can make the intent of your code clearer.

Using Environment.NewLine

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 of the program ===

First line
Second line

Using \r\n constant

Here 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.

=== Example 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 of the program ===

One line
Another line

Implementation

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.

L_000b: call string [mscorlib]System.Environment::get_NewLine()

Description of the intermediate language. 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.

See Intermediate Language (IL).

See Property Test.

Examining 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. This article is based on .NET 3.5 SP1. You can find more information on the usage and implementation of string literals in the C# programming language.

See String Literal.

.method public hidebysig specialname static string get_NewLine() cil managed
{
    .maxstack 8
    L_0000: ldstr "\r\n"
    L_0005: ret
}

Platform compatibility

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.

Summary

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, the author thinks that no one will ever forget what \r\n or \n mean, so the clarity is no better.

See String Overview.

© 2007-2010 Sam Allen. All rights reserved.

Dot Net Perls  Sam Allen