\ n和Environment.NewLine之间的区别

两者(如果有的话)之间有什么区别(相对于.Net)?



慕标5832272
浏览 1906回答 3
3回答

月关宝盒

取决于平台。在Windows上,它实际上是“ \ r \ n”。从MSDN:对于非Unix平台,包含“ \ r \ n”的字符串,对于Unix平台,包含“ \ n”的字符串。

泛舟湖上清波郎朗

Environment.NewLine从源代码的确切实现:.NET 4.6.1中的实现:/*===================================NewLine====================================**Action: A property which returns the appropriate newline string for the given**&nbsp; &nbsp; &nbsp; &nbsp; platform.**Returns: \r\n on Win32.**Arguments: None.**Exceptions: None.==============================================================================*/public static String NewLine {&nbsp; &nbsp; get {&nbsp; &nbsp; &nbsp; &nbsp; Contract.Ensures(Contract.Result<String>() != null);&nbsp; &nbsp; &nbsp; &nbsp; return "\r\n";&nbsp; &nbsp; }}资源.NET Core中的实现:/*===================================NewLine====================================**Action: A property which returns the appropriate newline string for the**&nbsp; &nbsp; &nbsp; &nbsp; given platform.**Returns: \r\n on Win32.**Arguments: None.**Exceptions: None.==============================================================================*/public static String NewLine {&nbsp; &nbsp; get {&nbsp; &nbsp; &nbsp; &nbsp; Contract.Ensures(Contract.Result() != null);#if !PLATFORM_UNIX&nbsp; &nbsp; &nbsp; &nbsp; return "\r\n";#else&nbsp; &nbsp; &nbsp; &nbsp; return "\n";#endif // !PLATFORM_UNIX&nbsp; &nbsp; }}来源(在中System.Private.CoreLib)public static string NewLine => "\r\n";来源(在中System.Runtime.Extensions)

青春有我

如其他人所述,Environment.NewLine返回平台特定的字符串以开始新行,该字符串应为:"\r\n" (\ u000D \ u000A)对于Windows"\n" (\ u000A)对于Unix"\r" (\ u000D)对于Mac(如果存在这种实现)请注意,在写入控制台时,并非严格要求Environment.NewLine。"\n"如果需要,控制台流将转换为适当的换行序列。
打开App,查看更多内容
随时随地看视频慕课网APP