Coder Perfect

The terms “n” and “environment” are not interchangeable. NewLine

Problem

What, if any, differences exist between the two (in terms of.Net)?

Asked by Prerak K

Solution #1

It is determined by the platform. It’s actually “rn” on Windows.

From MSDN:

Answered by anthony

Solution #2

Environment’s exact implementation. From the source code, a new line has been added:

In.NET 4.6.1, here’s how it’s done:

/*===================================NewLine====================================
**Action: A property which returns the appropriate newline string for the given
**        platform.
**Returns: \r\n on Win32.
**Arguments: None.
**Exceptions: None.
==============================================================================*/
public static String NewLine {
    get {
        Contract.Ensures(Contract.Result<String>() != null);
        return "\r\n";
    }
}

source

The.NET Core implementation is as follows:

/*===================================NewLine====================================
**Action: A property which returns the appropriate newline string for the
**        given platform.
**Returns: \r\n on Win32.
**Arguments: None.
**Exceptions: None.
==============================================================================*/
public static String NewLine {
    get {
        Contract.Ensures(Contract.Result() != null);
#if !PLATFORM_UNIX
        return "\r\n";
#else
        return "\n";
#endif // !PLATFORM_UNIX
    }
}

source (in System.Private.CoreLib)

public static string NewLine => "\r\n";

source (in System.Runtime.Extensions)

Answered by aloisdg

Solution #3

Environment, as others have stated. NewLine returns the following platform-specific string for starting a new line:

It’s worth noting that Environment.NewLine isn’t strictly required when writing to the console. If necessary, the console stream will translate “n” to the proper new-line sequence.

Answered by P Daddy

Solution #4

Environment. The newline character for the platform in which your code is running will be returned by NewLine.

This will come in handy when deploying your code on Linux using the Mono framework.

Answered by Rony

Solution #5

The following is taken directly from the documentation…

Answered by JP Alioto

Post is based on https://stackoverflow.com/questions/1015766/difference-between-n-and-environment-newline