views:

831

answers:

7

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

Thanks

+22  A: 

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
+3  A: 

From the docs ...

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

JP Alioto
+1 for the link
JeffH
+1  A: 

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
+8  A: 

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

Rony
+9  A: 

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
The Mac implementation does exist. Just Mono. Runs on anything.
Dykam
+1  A: 

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
A: 

Guys you don't seem to read the question. The question is not about "\r\n" but about "\n". I've seen it work in Windows. Maybe it was in the Console only - I'll need to check that. But if it works in Windows and according to the documentation it should work for Unix platforms too then what's the point of Environment.NewLine?

Iliyan