Environment.NewLine 属性

定义

获取为此环境定义的换行字符串。

C#
public static string NewLine { get; }

属性值

String

\r\n 对于非 Unix 平台或 \n Unix 平台。

示例

以下示例显示用换行符分隔的两行。

C#
// Sample for the Environment.NewLine property
using System;

class Sample
{
    public static void Main()
    {
        Console.WriteLine();
        Console.WriteLine($"NewLine: {Environment.NewLine}  first line{Environment.NewLine}  second line");
    }
}

/*
This example produces the following results:

NewLine:
  first line
  second line
*/

注解

属性值NewLine是针对当前平台和.NET Framework实现进行自定义的常量。 有关属性值中的转义字符的详细信息,请参阅 字符转义

提供 NewLine 的功能通常是换行符、换行符、换行符、回车符、CRLF 和行尾术语的含义。

NewLine可与特定于语言的新行支持结合使用,例如 Microsoft C# 和 C/C++ 中的转义字符“\r”和“\n”,也可以在 vbCrLf Microsoft Visual Basic中使用。

NewLine 自动追加到由 Console.WriteLineStringBuilder.AppendLine 方法处理的文本。

适用于

产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0