System.Console.Write 方法 (Object)

方法描述

将指定对象的文本表示形式写入标准输出流。

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

[HostProtectionAttribute(SecurityAction.LinkDemand, UI = true)]
public static void Write(
	Object value
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
value System-Object 要写入的值,或者为 null。
返回值 void

提示和注释

如果 value 为 null,不写入任何值并且不引发任何异常。 否则,value 的 ToString 方法被调用以生成其字符串表示,所得的字符串被写入标准输出流。

注意

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

System.Console.Write 方法 (Object)例子

下面的代码示例演示 Write 方法的用法。

public class FormatConverter {
    public static void Main(string[] args) {
        string lineInput;
        while ((lineInput = Console.ReadLine()) != null) {
            string[] fields = lineInput.Split(new char[] {'\t'});
            bool isFirstField = true;
            foreach(string item in fields) {
                if (isFirstField)
                    isFirstField = false;
                else
                    Console.Write(',');
                // If the field represents a boolean, replace with a numeric representation.
                try {
                    Console.Write(Convert.ToByte(Convert.ToBoolean(item)));
                }
                catch(FormatException) {
                    Console.Write(item);
                }
            }
            Console.WriteLine();
        }
    }
}

异常

异常 异常描述
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 系统要求。