这两者之间的区别是什么(如果有的话)?
当前回答
从医生那里…
包含“\r\n”的字符串 非unix平台,或者字符串 Unix平台包含“\n”。
其他回答
环境。NewLine在Windows上运行时会给出“\r\n”。如果你正在为基于Unix的环境生成字符串,你不需要“\r”。
从医生那里…
包含“\r\n”的字符串 非unix平台,或者字符串 Unix平台包含“\n”。
环境的精确实现。来自源代码的换行:
.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";
}
}
源
在。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(在System.Runtime.Extensions中)
这取决于平台。在Windows上,它实际上是“\r\n”。
从MSDN:
包含“\r\n”的字符串 非unix平台,或者字符串 Unix平台包含“\n”。
正如其他人提到的,环境。NewLine返回一个特定于平台的字符串,用于开始新行,它应该是:
“\r\n”(\u000D\u000A)用于Windows “\n”(\u000A)用于Unix “\r”(\u000D) for Mac(如果存在这样的实现)
注意,当写入控制台时,环境。NewLine不是必须的。如果需要,控制台流将“\n”转换为适当的新行序列。
推荐文章
- _ViewStart的位置和方式。CSHTML布局文件链接?
- 新建T()
- 如何将枚举绑定到WPF中的组合框控件?
- 拒绝访问该路径
- Visual Studio - Resx文件默认“内部”为“公共”
- 使用linq转换列表到字典,不用担心重复
- 单元测试:日期时间。现在
- 什么是回调?
- .NET中的KeyDown和KeyPress有什么区别?
- 返回匿名类型的结果?
- 你能解释一下流的概念吗?
- 如何在Java中监控计算机的CPU、内存和磁盘使用情况?
- 何时使用IList,何时使用List
- ConfigurationManager。AppSettings在.NET Core 2.0中可用?
- 在c#的控制台应用程序中使用'async