Difference between "\n" and Environment.NewLine
Asked Answered
I

7

251

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

Isomorphism answered 18/6, 2009 at 23:45 Comment(0)
M
234

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.

Ms answered 18/6, 2009 at 23:46 Comment(3)
Which UNIX platform does .NET run to Microsoft put into its doc? (I mean, a part from Mono.)Pentahedron
@Pentahedron 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 begunIgal
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.Counterattack
S
178

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)

Spiracle answered 5/4, 2014 at 8:34 Comment(2)
yeah :) For some reason I was expecting the .NET implementation to be some huge complicated functionEthiopic
Considering the simplicity of the source in this instance, it probably should be the answer.Corpse
G
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.

Griffiths answered 19/6, 2009 at 0:14 Comment(8)
The Mac implementation does exist. Just Mono. Runs on anything.Penny
Just a note, that would be old macs; new (OSX) macs use \nAttenweiler
@Dykam, yeah but it runs real slow-like on my HP48gxContinuant
@cod3monk3y: That's fast compared to my HP48sx!Dung
As others have mentioned and as is visible in the .Net source also attached to this question, this answer is not correct.Corpse
@ChrisWalter this answer could be correct back in 2009. Today, it is outdated.Spiracle
@Spiracle 2009? Apple hasn't used CR for 17 years; not since the initial release of macOS. This "non-Unix" thing is misleading. Windows is the only relevant OS that doesn't use LF. Linux, macOS, the BSDs, BeOS, RISC OS, and many more all use LFs.Stollings
@Stollings just saying that it might have been true in 2009, not that it was. I didnt check.Spiracle
F
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

Forzando answered 18/6, 2009 at 23:50 Comment(0)
A
8

From the docs ...

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

Alyssa answered 18/6, 2009 at 23:47 Comment(0)
V
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

Vastitude answered 19/6, 2009 at 13:35 Comment(0)
M
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".

Mosstrooper answered 18/6, 2009 at 23:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.