System.Console.WriteLine 方法

方法描述

将当前行终止符写入标准输出流。

语法定义(C# System.Console.WriteLine 方法 的用法)

[HostProtectionAttribute(SecurityAction.LinkDemand, UI = true)]
public static void WriteLine()

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
返回值 void

提示和注释

默认的行终止符是一个字符串,其值为一个回车符后面跟一个换行符(在 C# 中为 "\r\n",在 Visual Basic 中为 vbCrLf)。 可以通过将 Out 属性的 TextWriter.NewLine 属性设置为另一字符串来更改行终止符。 该示例提供了说明。

注意

应用到此类型或成员的 HostProtectionAttribute 特性具有以下 Resources 属性值:UI。HostProtectionAttribute 不影响桌面应用程序(桌面应用程序一般通过双击图标、键入命令或在浏览器中输入 URL 启动)。有关更多信息,请参见 HostProtectionAttribute 类或 SQL Server 编程和宿主保护特性。

System.Console.WriteLine 方法例子

然后,它调用 WriteLine() 和 WriteLine(String) 方法来将输出显示到控制台。

using System;

public class Example
{
   public static void Main()
   {
      string[] lines = { "This is the first line.", 
                         "This is the second line." };
      // Output the lines using the default newline sequence.
      Console.WriteLine("With the default new line characters:");
      Console.WriteLine();
      foreach (string line in lines)
         Console.WriteLine(line);

      Console.WriteLine();

      // Redefine the newline characters to double space.
      Console.Out.NewLine = "\r\n\r\n";
      // Output the lines using the new newline sequence.
      Console.WriteLine("With redefined new line characters:");
      Console.WriteLine();
      foreach (string line in lines)
         Console.WriteLine(line);
   }
}
// The example displays the following output:
//       With the default new line characters:
//       
//       This is the first line.
//       This is the second line.
//       
//       With redefined new line characters:
//       
//       
//       
//       This is the first line.
//       
//       This is the second line.

异常

异常 异常描述
IOException 发生了 I/O 错误。

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。