C# "\n" 和 Environment.NewLine 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1015766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between "\n" and Environment.NewLine
提问by Prerak K
What is the difference between two, if any (with respect to .Net)?
两者之间有什么区别(如果有的话)(关于 .Net)?
采纳答案by anthony
Depends on the platform. On Windows it is actually "\r\n".
取决于平台。在 Windows 上,它实际上是“\r\n”。
From MSDN:
来自 MSDN:
A string containing "\r\n" for non-Unix platforms, or a string containing "\n" for Unix platforms.
包含“\r\n”的字符串用于非 Unix 平台,或包含“\n”的字符串用于 Unix 平台。
回答by JP Alioto
回答by Jeff Meatball Yang
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".
Environment.NewLine 在 Windows 上运行时会给出 "\r\n"。如果您要为基于 Unix 的环境生成字符串,则不需要“\r”。
回答by Rony
回答by P Daddy
As others have mentioned, Environment.NewLine
returns a platform-specific string for beginning a new line, which should be:
正如其他人所提到的,Environment.NewLine
返回一个用于开始新行的特定于平台的字符串,它应该是:
"\r\n"
(\u000D\u000A) for Windows"\n"
(\u000A) for Unix"\r"
(\u000D) for Mac (if such implementation existed)
"\r\n"
(\u000D\u000A) 用于 Windows"\n"
(\u000A) 用于 Unix"\r"
(\u000D) for Mac(如果存在这样的实现)
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.
请注意,写入控制台时,Environment.NewLine 并不是绝对必要的。"\n"
如有必要,控制台流将转换为适当的换行序列。
回答by Yahya
You might get into trouble when you try to display multi-line message separated with "\r\n".
当您尝试显示以“\r\n”分隔的多行消息时,您可能会遇到麻烦。
It is always a good practice to do things in a standard way, and use Environment.NewLine
以标准方式做事并使用 Environment.NewLine 始终是一个好习惯
回答by aloisdg moving to codidact.com
Exact implementation of Environment.NewLine
from the source code:
Environment.NewLine
从源代码的确切实现:
The implementation in .NET 4.6.1:
.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";
}
}
The implementation in .NET Core:
.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
)
来源(在System.Private.CoreLib
)
public static string NewLine => "\r\n";
source(in System.Runtime.Extensions
)
来源(在System.Runtime.Extensions
)