246

What is the difference between two, if any (with respect to .Net)?

pb2q
  • 58,613
  • 19
  • 146
  • 147
Prerak K
  • 10,940
  • 7
  • 30
  • 37

7 Answers7

231

Depends on the platform. On Windows it is actually "\r\n".

From MSDN:

A string containing "\r\n" for non-Unix platforms, or a string containing "\n" for Unix platforms.

anthony
  • 40,424
  • 5
  • 55
  • 128
  • 5
    Which UNIX platform does .NET run to Microsoft put into its doc? (I mean, a part from Mono.) – Jack Jul 27 '14 at 04:24
  • 13
    @Jack You could be writing to a file that will be opened in Unix platform. Or sending text in package that will be received by a unix platform. And in a few months .net will be running on Unix platforms. It already has begun – John Demetriou May 01 '15 at 13:36
  • So just for clarity: on a non-Unix platform `Environment.NewLine` is `\r\n` but `\n` is also called "new line". Why didn't they just call the latter by its more well-known name "line feed" and cut out the confusion? They could have used `\l` also. – rory.ap Sep 26 '18 at 15:52
177

Exact implementation of Environment.NewLine from the source code:

The implementation in .NET 4.6.1:

/*===================================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 implementation in .NET Core:

/*===================================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)

aloisdg
  • 22,270
  • 6
  • 85
  • 105
70

As others have mentioned, Environment.NewLine returns a platform-specific string for beginning a new line, which should be:

  • "\r\n" (\u000D\u000A) for Windows
  • "\n" (\u000A) for Unix
  • "\r" (\u000D) for Mac (if such implementation existed)

Note that when writing to the console, Environment.NewLine is not strictly necessary. The console stream will translate "\n" to the appropriate new-line sequence, if necessary.

P Daddy
  • 28,912
  • 9
  • 68
  • 92
27

Environment.NewLine will return the newline character for the corresponding platform in which your code is running

you will find this very useful when you deploy your code in linux on the Mono framework

George G
  • 7,443
  • 12
  • 45
  • 59
Rony
  • 9,331
  • 2
  • 22
  • 22
8

From the docs ...

A string containing "\r\n" for non-Unix platforms, or a string containing "\n" for Unix platforms.

JP Alioto
  • 44,864
  • 6
  • 88
  • 112
5

You might get into trouble when you try to display multi-line message separated with "\r\n".

It is always a good practice to do things in a standard way, and use Environment.NewLine

Yahya
  • 3,386
  • 3
  • 22
  • 40
4

Environment.NewLine will give "\r\n" when run on Windows. If you are generating strings for Unix based environments, you don't want the "\r".

Jeff Meatball Yang
  • 37,839
  • 27
  • 91
  • 125