使用英语阅读

通过


Console.Write 方法

定义

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

重载

Write(String, Object, Object, Object, Object)

使用指定的格式信息将指定对象和可变长度参数列表的文本表示形式写入标准输出流。

Write(String, Object, Object, Object)

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

Write(Char[], Int32, Int32)

将 Unicode 字符的指定子数组写入标准输出流。

Write(String, ReadOnlySpan<Object>)

使用指定的格式信息将指定对象范围的文本表示形式写入标准输出流。

Write(String, Object[])

使用指定的格式信息将指定对象数组的文本表示形式写入标准输出流。

Write(String, Object)

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

Write(UInt64)

将指定的 64 位无符号整数值的文本表示形式写入标准输出流。

Write(UInt32)

将指定 32 位无符号整数值的文本表示形式写入标准输出流。

Write(String)

将指定的字符串值写入标准输出流。

Write(String, Object, Object)

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

Write(Object)

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

Write(Single)

将指定单精度浮点值的文本表示形式写入标准输出流。

Write(Char)

将指定的 Unicode 字符值写入标准输出流。

Write(Char[])

将指定的 Unicode 字符数组写入标准输出流。

Write(Boolean)

将指定布尔值的文本表示形式写入标准输出流。

Write(Double)

将指定双精度浮点值的文本表示形式写入标准输出流。

Write(Int32)

将指定 32 位有符号整数值的文本表示形式写入标准输出流。

Write(Int64)

将指定 64 位带符号整数值的文本表示形式写入标准输出流。

Write(Decimal)

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

Write(String, Object, Object, Object, Object)

重要

此 API 不符合 CLS。

使用指定的格式信息将指定对象和可变长度参数列表的文本表示形式写入标准输出流。

[System.CLSCompliant(false)]
public static void Write (string format, object arg0, object arg1, object arg2, object arg3);

参数

format
String

复合格式字符串。

arg0
Object

使用 format写入的第一个对象。

arg1
Object

使用 format写入的第二个对象。

arg2
Object

使用 format写入的第三个对象。

arg3
Object

使用 format写入的第四个对象。

属性

例外

出现 I/O 错误。

format null

format 中的格式规范无效。

示例

以下示例演示如何将变量参数与 Write(String, Object, Object, Object, Object) 方法一起使用。 该方法使用复合格式字符串和五个格式项调用。

using namespace System;

//void main(array<System::String ^> ^args)
void main()
{
   DateTime dateRecorded(2009, 6, 15);
   DateTime startTime(1, 1, 1, 0, 30, 0);
   TimeSpan interval(12, 0, 0);

   Double temperature1 = 52.8;
   Double temperature2 = 63.5;
   
   Console::Write("Date: {0:d}:\n   Temperature at {1:t}: {2}\n   Temperature at {3:t}: {4}\n", 
                  dateRecorded, startTime, temperature1, 
                  startTime.Add(interval), temperature2);
    Console::ReadLine();
}
// The example displays the following output:
//      Date: 6/15/2009:
//         Temperature at 12:30 AM: 52.8
//         Temperature at 12:30 PM: 63.5

注解

备注

此 API 不符合 CLS。 符合 CLS 的替代方法是 Console.Write(String, Object[])。 C# 和 Visual Basic 编译器会自动解析对此方法的调用作为对 Console.Write(String, Object[])的调用。

此方法使用 .NET 复合格式设置功能将对象的值转换为其文本表示形式,并将该表示形式嵌入字符串中。 生成的字符串将写入输出流。

format 参数包含与零个或多个索引占位符(称为格式项)混合的文本运行,这些占位符与此方法的参数列表中的对象相对应。 格式设置过程将每个格式项替换为相应对象值的文本表示形式。

格式项的语法 {索引[,对齐方式][:formatString]},它指定强制索引、格式化文本的可选长度和对齐方式,以及控制相应对象值的格式说明符字符的可选字符串。

.NET 提供广泛的格式支持,在以下格式设置主题中对此进行了更详细的介绍。

调用方说明

此方法使用 vararg 关键字进行标记,这意味着它支持可变数量的参数。 可以从 Visual C++调用该方法,但不能从 C# 或 Visual Basic 代码调用该方法。 C# 和 Visual Basic 编译器将对 Write(String, Object, Object, Object, Object) 的调用解析为对 Write(String, Object[])的调用。

另请参阅

适用于

Write(String, Object, Object, Object)

Source:
Console.cs
Source:
Console.cs
Source:
Console.cs

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

public static void Write (string format, object? arg0, object? arg1, object? arg2);
public static void Write (string format, object arg0, object arg1, object arg2);

参数

format
String

复合格式字符串。

arg0
Object

使用 format写入的第一个对象。

arg1
Object

使用 format写入的第二个对象。

arg2
Object

使用 format写入的第三个对象。

例外

出现 I/O 错误。

format null

format 中的格式规范无效。

示例

以下示例使用 WriteLine 方法演示数字、日期和枚举的标准格式说明符。

// This code example demonstrates the Console.WriteLine() method.
// Formatting for this example uses the "en-US" culture.

using System;
class Sample
{
    enum Color {Yellow = 1, Blue, Green};
    static DateTime thisDate = DateTime.Now;

    public static void Main()
    {
        Console.Clear();

        // Format a negative integer or floating-point number in various ways.
        Console.WriteLine("Standard Numeric Format Specifiers");
        Console.WriteLine(
            "(C) Currency: . . . . . . . . {0:C}\n" +
            "(D) Decimal:. . . . . . . . . {0:D}\n" +
            "(E) Scientific: . . . . . . . {1:E}\n" +
            "(F) Fixed point:. . . . . . . {1:F}\n" +
            "(G) General:. . . . . . . . . {0:G}\n" +
            "    (default):. . . . . . . . {0} (default = 'G')\n" +
            "(N) Number: . . . . . . . . . {0:N}\n" +
            "(P) Percent:. . . . . . . . . {1:P}\n" +
            "(R) Round-trip: . . . . . . . {1:R}\n" +
            "(X) Hexadecimal:. . . . . . . {0:X}\n",
            -123, -123.45f);

        // Format the current date in various ways.
        Console.WriteLine("Standard DateTime Format Specifiers");
        Console.WriteLine(
            "(d) Short date: . . . . . . . {0:d}\n" +
            "(D) Long date:. . . . . . . . {0:D}\n" +
            "(t) Short time: . . . . . . . {0:t}\n" +
            "(T) Long time:. . . . . . . . {0:T}\n" +
            "(f) Full date/short time: . . {0:f}\n" +
            "(F) Full date/long time:. . . {0:F}\n" +
            "(g) General date/short time:. {0:g}\n" +
            "(G) General date/long time: . {0:G}\n" +
            "    (default):. . . . . . . . {0} (default = 'G')\n" +
            "(M) Month:. . . . . . . . . . {0:M}\n" +
            "(R) RFC1123:. . . . . . . . . {0:R}\n" +
            "(s) Sortable: . . . . . . . . {0:s}\n" +
            "(u) Universal sortable: . . . {0:u} (invariant)\n" +
            "(U) Universal full date/time: {0:U}\n" +
            "(Y) Year: . . . . . . . . . . {0:Y}\n",
            thisDate);

        // Format a Color enumeration value in various ways.
        Console.WriteLine("Standard Enumeration Format Specifiers");
        Console.WriteLine(
            "(G) General:. . . . . . . . . {0:G}\n" +
            "    (default):. . . . . . . . {0} (default = 'G')\n" +
            "(F) Flags:. . . . . . . . . . {0:F} (flags or integer)\n" +
            "(D) Decimal number: . . . . . {0:D}\n" +
            "(X) Hexadecimal:. . . . . . . {0:X}\n",
            Color.Green);
    }
}
/*
This code example produces the following results:

Standard Numeric Format Specifiers
(C) Currency: . . . . . . . . ($123.00)
(D) Decimal:. . . . . . . . . -123
(E) Scientific: . . . . . . . -1.234500E+002
(F) Fixed point:. . . . . . . -123.45
(G) General:. . . . . . . . . -123
    (default):. . . . . . . . -123 (default = 'G')
(N) Number: . . . . . . . . . -123.00
(P) Percent:. . . . . . . . . -12,345.00 %
(R) Round-trip: . . . . . . . -123.45
(X) Hexadecimal:. . . . . . . FFFFFF85

Standard DateTime Format Specifiers
(d) Short date: . . . . . . . 6/26/2004
(D) Long date:. . . . . . . . Saturday, June 26, 2004
(t) Short time: . . . . . . . 8:11 PM
(T) Long time:. . . . . . . . 8:11:04 PM
(f) Full date/short time: . . Saturday, June 26, 2004 8:11 PM
(F) Full date/long time:. . . Saturday, June 26, 2004 8:11:04 PM
(g) General date/short time:. 6/26/2004 8:11 PM
(G) General date/long time: . 6/26/2004 8:11:04 PM
    (default):. . . . . . . . 6/26/2004 8:11:04 PM (default = 'G')
(M) Month:. . . . . . . . . . June 26
(R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT
(s) Sortable: . . . . . . . . 2004-06-26T20:11:04
(u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant)
(U) Universal full date/time: Sunday, June 27, 2004 3:11:04 AM
(Y) Year: . . . . . . . . . . June, 2004

Standard Enumeration Format Specifiers
(G) General:. . . . . . . . . Green
    (default):. . . . . . . . Green (default = 'G')
(F) Flags:. . . . . . . . . . Green (flags or integer)
(D) Decimal number: . . . . . 3
(X) Hexadecimal:. . . . . . . 00000003

*/

以下示例演示了如何使用 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 (var item in fields)
            {
                if (isFirstField)
                    isFirstField = false;
                else
                    Console.Write(',');

                // If the field represents a boolean, replace with a numeric representation.
                bool itemBool;
                if (Boolean.TryParse(item, out itemBool))
                    Console.Write(Convert.ToByte(itemBool));
                else
                    Console.Write(item);
            }
            Console.WriteLine();
        }
    }
}

注解

此方法使用 .NET 复合格式设置功能将对象的值转换为其文本表示形式,并将该表示形式嵌入字符串中。 生成的字符串将写入输出流。

format 参数包含与零个或多个索引占位符(称为格式项)混合的文本运行,这些占位符与此方法的参数列表中的对象相对应。 格式设置过程将每个格式项替换为相应对象值的文本表示形式。

格式项的语法 {索引[,对齐方式][:formatString]},它指定强制索引、格式化文本的可选长度和对齐方式,以及控制相应对象值的格式说明符字符的可选字符串。

.NET 提供广泛的格式支持,在以下格式设置主题中对此进行了更详细的介绍。

另请参阅

适用于

Write(Char[], Int32, Int32)

Source:
Console.cs
Source:
Console.cs
Source:
Console.cs

将 Unicode 字符的指定子数组写入标准输出流。

public static void Write (char[] buffer, int index, int count);

参数

buffer
Char[]

Unicode 字符数组。

index
Int32

buffer中的起始位置。

count
Int32

要写入的字符数。

例外

buffer null

indexcount 小于零。

indexcount 指定不在 buffer内的位置。

出现 I/O 错误。

注解

此方法将 count 字符从 buffer 的位置 index 写入标准输出流。

另请参阅

适用于

Write(String, ReadOnlySpan<Object>)

使用指定的格式信息将指定对象范围的文本表示形式写入标准输出流。

public static void Write (string format, scoped ReadOnlySpan<object?> arg);

参数

format
String

复合格式字符串。

arg
ReadOnlySpan<Object>

使用格式写入的对象范围。

适用于

Write(String, Object[])

Source:
Console.cs
Source:
Console.cs
Source:
Console.cs

使用指定的格式信息将指定对象数组的文本表示形式写入标准输出流。

public static void Write (string format, params object?[]? arg);
public static void Write (string format, params object[] arg);

参数

format
String

复合格式字符串。

arg
Object[]

使用 format写入的对象数组。

例外

出现 I/O 错误。

formatargnull

format 中的格式规范无效。

示例

以下示例定义一个 Person 类,其中包含许多提供有关人员的信息的属性。 其 GetDescription 方法返回一个数组,其中包含除一个属性值以外的所有属性值。 然后,该示例使用 GetDescription 方法返回的数组来显示 Person 对象的值。

using System;

public class Person
{
   public String Name { get; set; }
   public DateTime BirthDate  { get; set; }
   public Double Height { get; set; }
   public Double Weight { get; set; }
   public Char Gender { get; set; }
   public String Remarks { get; set; }

   public object[] GetDescription()
   {
      return new object[] { Name, Gender, Height, Weight, BirthDate};
   }
}

public class Example
{
   public static void Main()
   {
      var p1 = new Person() { Name = "John", Gender = 'M',
                              BirthDate = new DateTime(1992, 5, 10),
                              Height = 73.5, Weight = 207 };
      p1.Remarks = "Client since 1/3/2012";
      Console.Write("{0}: {1}, born {4:d}  Height {2} inches, Weight {3} lbs  ",
                    p1.GetDescription());
      if (String.IsNullOrEmpty(p1.Remarks))
         Console.WriteLine();
      else
         Console.WriteLine("{1}Remarks: {0}", p1.Remarks,
                           Console.CursorLeft + p1.Remarks.Length + 10 > Console.WindowWidth ?
                              "\n   " : "");
   }
}
// The example displays the following output:
//    John: M, born 5/10/1992  Height 73.5 inches, Weight 207 lbs  Remarks: Client since 1/3/2012

请注意,该示例调用 Write(String, Object[]) 方法,而不是 WriteLine(String, Object[]) 方法,因为它尝试在同一行上显示 Person.Remarks 属性的值。 为此,它会检查 CursorLeftWindowWidth 属性的值,以确定是否有足够的空间容纳该备注。 如果有,则显示该行。 如果没有,它将写入一行,缩进三个空格,并显示备注。

以下示例与第一个示例相同,只不过它提供五项列表作为 arg 参数而不是参数数组。

using System;

public class Person
{
   public String Name { get; set; }
   public DateTime BirthDate  { get; set; }
   public Double Height { get; set; }
   public Double Weight { get; set; }
   public Char Gender { get; set; }
   public String Remarks { get; set; }

   public object[] GetDescription()
   {
      return new object[] { Name, Gender, Height, Weight, BirthDate};
   }
}

public class Example
{
   public static void Main()
   {
      var p1 = new Person() { Name = "John", Gender = 'M',
                              BirthDate = new DateTime(1992, 5, 10),
                              Height = 73.5, Weight = 207 };
      p1.Remarks = "Client since 1/3/2012";
      Console.Write("{0}: {1}, born {2:d}  Height {3} inches, Weight {4} lbs  ",
                    p1.Name, p1.Gender, p1.BirthDate, p1.Height, p1.Weight);
      if (String.IsNullOrEmpty(p1.Remarks))
         Console.WriteLine();
      else
         Console.WriteLine("{1}Remarks: {0}", p1.Remarks,
                           Console.CursorLeft + p1.Remarks.Length + 10 > Console.WindowWidth ?
                              "\n   " : "");
   }
}
// The example displays the following output:
//    John: M, born 5/10/1992  Height 73.5 inches, Weight 207 lbs  Remarks: Client since 1/3/2012

注解

此方法使用 .NET 复合格式设置功能将对象的值转换为其文本表示形式,并将该表示形式嵌入字符串中。 生成的字符串将写入输出流。

format 参数包含与零个或多个索引占位符(称为格式项)混合的文本运行,这些占位符与此方法的参数列表中的对象相对应。 格式设置过程将每个格式项替换为相应对象值的文本表示形式。

格式项的语法 {索引[,对齐方式][:formatString]},它指定强制索引、格式化文本的可选长度和对齐方式,以及控制相应对象值的格式说明符字符的可选字符串。

.NET 提供广泛的格式支持,在以下格式设置主题中对此进行了更详细的介绍。

arg 参数是参数数组。 可以将参数作为数组传递给方法,也可以作为五个或多个项的列表传递给该方法。 这些示例说明了这两种形式的方法调用。

调用方说明

C++代码不调用此方法。 C++编译器将调用 System.Console.Write 解析为调用 Write(String, Object, Object, Object, Object)的字符串和四个或多个对象参数的列表。 它解析对 System.Console.Write 的调用,其中包括字符串和对象数组作为对 Write(String, Object)的调用。

另请参阅

适用于

Write(String, Object)

Source:
Console.cs
Source:
Console.cs
Source:
Console.cs

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

public static void Write (string format, object? arg0);
public static void Write (string format, object arg0);

参数

format
String

复合格式字符串。

arg0
Object

使用 format写入的对象。

例外

出现 I/O 错误。

format null

format 中的格式规范无效。

示例

以下示例使用 WriteLine 方法演示数字、日期和枚举的标准格式说明符。

// This code example demonstrates the Console.WriteLine() method.
// Formatting for this example uses the "en-US" culture.

using System;
class Sample
{
    enum Color {Yellow = 1, Blue, Green};
    static DateTime thisDate = DateTime.Now;

    public static void Main()
    {
        Console.Clear();

        // Format a negative integer or floating-point number in various ways.
        Console.WriteLine("Standard Numeric Format Specifiers");
        Console.WriteLine(
            "(C) Currency: . . . . . . . . {0:C}\n" +
            "(D) Decimal:. . . . . . . . . {0:D}\n" +
            "(E) Scientific: . . . . . . . {1:E}\n" +
            "(F) Fixed point:. . . . . . . {1:F}\n" +
            "(G) General:. . . . . . . . . {0:G}\n" +
            "    (default):. . . . . . . . {0} (default = 'G')\n" +
            "(N) Number: . . . . . . . . . {0:N}\n" +
            "(P) Percent:. . . . . . . . . {1:P}\n" +
            "(R) Round-trip: . . . . . . . {1:R}\n" +
            "(X) Hexadecimal:. . . . . . . {0:X}\n",
            -123, -123.45f);

        // Format the current date in various ways.
        Console.WriteLine("Standard DateTime Format Specifiers");
        Console.WriteLine(
            "(d) Short date: . . . . . . . {0:d}\n" +
            "(D) Long date:. . . . . . . . {0:D}\n" +
            "(t) Short time: . . . . . . . {0:t}\n" +
            "(T) Long time:. . . . . . . . {0:T}\n" +
            "(f) Full date/short time: . . {0:f}\n" +
            "(F) Full date/long time:. . . {0:F}\n" +
            "(g) General date/short time:. {0:g}\n" +
            "(G) General date/long time: . {0:G}\n" +
            "    (default):. . . . . . . . {0} (default = 'G')\n" +
            "(M) Month:. . . . . . . . . . {0:M}\n" +
            "(R) RFC1123:. . . . . . . . . {0:R}\n" +
            "(s) Sortable: . . . . . . . . {0:s}\n" +
            "(u) Universal sortable: . . . {0:u} (invariant)\n" +
            "(U) Universal full date/time: {0:U}\n" +
            "(Y) Year: . . . . . . . . . . {0:Y}\n",
            thisDate);

        // Format a Color enumeration value in various ways.
        Console.WriteLine("Standard Enumeration Format Specifiers");
        Console.WriteLine(
            "(G) General:. . . . . . . . . {0:G}\n" +
            "    (default):. . . . . . . . {0} (default = 'G')\n" +
            "(F) Flags:. . . . . . . . . . {0:F} (flags or integer)\n" +
            "(D) Decimal number: . . . . . {0:D}\n" +
            "(X) Hexadecimal:. . . . . . . {0:X}\n",
            Color.Green);
    }
}
/*
This code example produces the following results:

Standard Numeric Format Specifiers
(C) Currency: . . . . . . . . ($123.00)
(D) Decimal:. . . . . . . . . -123
(E) Scientific: . . . . . . . -1.234500E+002
(F) Fixed point:. . . . . . . -123.45
(G) General:. . . . . . . . . -123
    (default):. . . . . . . . -123 (default = 'G')
(N) Number: . . . . . . . . . -123.00
(P) Percent:. . . . . . . . . -12,345.00 %
(R) Round-trip: . . . . . . . -123.45
(X) Hexadecimal:. . . . . . . FFFFFF85

Standard DateTime Format Specifiers
(d) Short date: . . . . . . . 6/26/2004
(D) Long date:. . . . . . . . Saturday, June 26, 2004
(t) Short time: . . . . . . . 8:11 PM
(T) Long time:. . . . . . . . 8:11:04 PM
(f) Full date/short time: . . Saturday, June 26, 2004 8:11 PM
(F) Full date/long time:. . . Saturday, June 26, 2004 8:11:04 PM
(g) General date/short time:. 6/26/2004 8:11 PM
(G) General date/long time: . 6/26/2004 8:11:04 PM
    (default):. . . . . . . . 6/26/2004 8:11:04 PM (default = 'G')
(M) Month:. . . . . . . . . . June 26
(R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT
(s) Sortable: . . . . . . . . 2004-06-26T20:11:04
(u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant)
(U) Universal full date/time: Sunday, June 27, 2004 3:11:04 AM
(Y) Year: . . . . . . . . . . June, 2004

Standard Enumeration Format Specifiers
(G) General:. . . . . . . . . Green
    (default):. . . . . . . . Green (default = 'G')
(F) Flags:. . . . . . . . . . Green (flags or integer)
(D) Decimal number: . . . . . 3
(X) Hexadecimal:. . . . . . . 00000003

*/

以下示例演示了如何使用 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 (var item in fields)
            {
                if (isFirstField)
                    isFirstField = false;
                else
                    Console.Write(',');

                // If the field represents a boolean, replace with a numeric representation.
                bool itemBool;
                if (Boolean.TryParse(item, out itemBool))
                    Console.Write(Convert.ToByte(itemBool));
                else
                    Console.Write(item);
            }
            Console.WriteLine();
        }
    }
}

注解

此方法使用 .NET 的 复合格式设置功能 将对象的值转换为其文本表示形式,并将该表示形式嵌入字符串中。 生成的字符串将写入输出流。

format 参数包含与零个或多个索引占位符(称为格式项)混合的文本运行,这些占位符与此方法的参数列表中的对象相对应。 格式设置过程将每个格式项替换为相应对象值的文本表示形式。

格式项的语法 {索引[,对齐方式][:formatString]},它指定强制索引、格式化文本的可选长度和对齐方式,以及控制相应对象值的格式说明符字符的可选字符串。

.NET 提供广泛的格式支持,在以下格式设置主题中对此进行了更详细的介绍。

另请参阅

适用于

Write(UInt64)

Source:
Console.cs
Source:
Console.cs
Source:
Console.cs

重要

此 API 不符合 CLS。

将指定的 64 位无符号整数值的文本表示形式写入标准输出流。

[System.CLSCompliant(false)]
public static void Write (ulong value);

参数

value
UInt64

要写入的值。

属性

例外

出现 I/O 错误。

示例

以下示例演示了如何使用 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 (var item in fields)
            {
                if (isFirstField)
                    isFirstField = false;
                else
                    Console.Write(',');

                // If the field represents a boolean, replace with a numeric representation.
                bool itemBool;
                if (Boolean.TryParse(item, out itemBool))
                    Console.Write(Convert.ToByte(itemBool));
                else
                    Console.Write(item);
            }
            Console.WriteLine();
        }
    }
}

注解

value 的文本表示形式是通过调用 UInt64.ToString 方法生成的。

另请参阅

适用于

Write(UInt32)

Source:
Console.cs
Source:
Console.cs
Source:
Console.cs

重要

此 API 不符合 CLS。

将指定 32 位无符号整数值的文本表示形式写入标准输出流。

[System.CLSCompliant(false)]
public static void Write (uint value);

参数

value
UInt32

要写入的值。

属性

例外

出现 I/O 错误。

示例

以下示例演示了如何使用 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 (var item in fields)
            {
                if (isFirstField)
                    isFirstField = false;
                else
                    Console.Write(',');

                // If the field represents a boolean, replace with a numeric representation.
                bool itemBool;
                if (Boolean.TryParse(item, out itemBool))
                    Console.Write(Convert.ToByte(itemBool));
                else
                    Console.Write(item);
            }
            Console.WriteLine();
        }
    }
}

注解

value 的文本表示形式是通过调用 UInt32.ToString 方法生成的。

另请参阅

适用于

Write(String)

Source:
Console.cs
Source:
Console.cs
Source:
Console.cs

将指定的字符串值写入标准输出流。

public static void Write (string? value);
public static void Write (string value);

参数

value
String

要写入的值。

例外

出现 I/O 错误。

示例

以下示例演示了如何使用 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 (var item in fields)
            {
                if (isFirstField)
                    isFirstField = false;
                else
                    Console.Write(',');

                // If the field represents a boolean, replace with a numeric representation.
                bool itemBool;
                if (Boolean.TryParse(item, out itemBool))
                    Console.Write(Convert.ToByte(itemBool));
                else
                    Console.Write(item);
            }
            Console.WriteLine();
        }
    }
}

注解

如果值 null,则不会写入标准输出流。

另请参阅

适用于

Write(String, Object, Object)

Source:
Console.cs
Source:
Console.cs
Source:
Console.cs

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

public static void Write (string format, object? arg0, object? arg1);
public static void Write (string format, object arg0, object arg1);

参数

format
String

复合格式字符串。

arg0
Object

使用 format写入的第一个对象。

arg1
Object

使用 format写入的第二个对象。

例外

出现 I/O 错误。

format null

format 中的格式规范无效。

示例

以下示例使用 WriteLine 方法演示数字、日期和枚举的标准格式说明符。

// This code example demonstrates the Console.WriteLine() method.
// Formatting for this example uses the "en-US" culture.

using System;
class Sample
{
    enum Color {Yellow = 1, Blue, Green};
    static DateTime thisDate = DateTime.Now;

    public static void Main()
    {
        Console.Clear();

        // Format a negative integer or floating-point number in various ways.
        Console.WriteLine("Standard Numeric Format Specifiers");
        Console.WriteLine(
            "(C) Currency: . . . . . . . . {0:C}\n" +
            "(D) Decimal:. . . . . . . . . {0:D}\n" +
            "(E) Scientific: . . . . . . . {1:E}\n" +
            "(F) Fixed point:. . . . . . . {1:F}\n" +
            "(G) General:. . . . . . . . . {0:G}\n" +
            "    (default):. . . . . . . . {0} (default = 'G')\n" +
            "(N) Number: . . . . . . . . . {0:N}\n" +
            "(P) Percent:. . . . . . . . . {1:P}\n" +
            "(R) Round-trip: . . . . . . . {1:R}\n" +
            "(X) Hexadecimal:. . . . . . . {0:X}\n",
            -123, -123.45f);

        // Format the current date in various ways.
        Console.WriteLine("Standard DateTime Format Specifiers");
        Console.WriteLine(
            "(d) Short date: . . . . . . . {0:d}\n" +
            "(D) Long date:. . . . . . . . {0:D}\n" +
            "(t) Short time: . . . . . . . {0:t}\n" +
            "(T) Long time:. . . . . . . . {0:T}\n" +
            "(f) Full date/short time: . . {0:f}\n" +
            "(F) Full date/long time:. . . {0:F}\n" +
            "(g) General date/short time:. {0:g}\n" +
            "(G) General date/long time: . {0:G}\n" +
            "    (default):. . . . . . . . {0} (default = 'G')\n" +
            "(M) Month:. . . . . . . . . . {0:M}\n" +
            "(R) RFC1123:. . . . . . . . . {0:R}\n" +
            "(s) Sortable: . . . . . . . . {0:s}\n" +
            "(u) Universal sortable: . . . {0:u} (invariant)\n" +
            "(U) Universal full date/time: {0:U}\n" +
            "(Y) Year: . . . . . . . . . . {0:Y}\n",
            thisDate);

        // Format a Color enumeration value in various ways.
        Console.WriteLine("Standard Enumeration Format Specifiers");
        Console.WriteLine(
            "(G) General:. . . . . . . . . {0:G}\n" +
            "    (default):. . . . . . . . {0} (default = 'G')\n" +
            "(F) Flags:. . . . . . . . . . {0:F} (flags or integer)\n" +
            "(D) Decimal number: . . . . . {0:D}\n" +
            "(X) Hexadecimal:. . . . . . . {0:X}\n",
            Color.Green);
    }
}
/*
This code example produces the following results:

Standard Numeric Format Specifiers
(C) Currency: . . . . . . . . ($123.00)
(D) Decimal:. . . . . . . . . -123
(E) Scientific: . . . . . . . -1.234500E+002
(F) Fixed point:. . . . . . . -123.45
(G) General:. . . . . . . . . -123
    (default):. . . . . . . . -123 (default = 'G')
(N) Number: . . . . . . . . . -123.00
(P) Percent:. . . . . . . . . -12,345.00 %
(R) Round-trip: . . . . . . . -123.45
(X) Hexadecimal:. . . . . . . FFFFFF85

Standard DateTime Format Specifiers
(d) Short date: . . . . . . . 6/26/2004
(D) Long date:. . . . . . . . Saturday, June 26, 2004
(t) Short time: . . . . . . . 8:11 PM
(T) Long time:. . . . . . . . 8:11:04 PM
(f) Full date/short time: . . Saturday, June 26, 2004 8:11 PM
(F) Full date/long time:. . . Saturday, June 26, 2004 8:11:04 PM
(g) General date/short time:. 6/26/2004 8:11 PM
(G) General date/long time: . 6/26/2004 8:11:04 PM
    (default):. . . . . . . . 6/26/2004 8:11:04 PM (default = 'G')
(M) Month:. . . . . . . . . . June 26
(R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT
(s) Sortable: . . . . . . . . 2004-06-26T20:11:04
(u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant)
(U) Universal full date/time: Sunday, June 27, 2004 3:11:04 AM
(Y) Year: . . . . . . . . . . June, 2004

Standard Enumeration Format Specifiers
(G) General:. . . . . . . . . Green
    (default):. . . . . . . . Green (default = 'G')
(F) Flags:. . . . . . . . . . Green (flags or integer)
(D) Decimal number: . . . . . 3
(X) Hexadecimal:. . . . . . . 00000003

*/

以下示例演示了如何使用 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 (var item in fields)
            {
                if (isFirstField)
                    isFirstField = false;
                else
                    Console.Write(',');

                // If the field represents a boolean, replace with a numeric representation.
                bool itemBool;
                if (Boolean.TryParse(item, out itemBool))
                    Console.Write(Convert.ToByte(itemBool));
                else
                    Console.Write(item);
            }
            Console.WriteLine();
        }
    }
}

注解

此方法使用 .NET 复合格式设置功能将对象的值转换为其文本表示形式,并将该表示形式嵌入字符串中。 生成的字符串将写入输出流。

format 参数包含与零个或多个索引占位符(称为格式项)混合的文本运行,这些占位符与此方法的参数列表中的对象相对应。 格式设置过程将每个格式项替换为相应对象值的文本表示形式。

格式项的语法 {索引[,对齐方式][:formatString]},它指定强制索引、格式化文本的可选长度和对齐方式,以及控制相应对象值的格式说明符字符的可选字符串。

.NET 提供广泛的格式支持,在以下格式设置主题中对此进行了更详细的介绍。

另请参阅

适用于

Write(Object)

Source:
Console.cs
Source:
Console.cs
Source:
Console.cs

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

public static void Write (object? value);
public static void Write (object value);

参数

value
Object

要写入的值,或 null

例外

出现 I/O 错误。

示例

以下示例演示了如何使用 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 (var item in fields)
            {
                if (isFirstField)
                    isFirstField = false;
                else
                    Console.Write(',');

                // If the field represents a boolean, replace with a numeric representation.
                bool itemBool;
                if (Boolean.TryParse(item, out itemBool))
                    Console.Write(Convert.ToByte(itemBool));
                else
                    Console.Write(item);
            }
            Console.WriteLine();
        }
    }
}

注解

如果 valuenull,则不会写入任何内容,也不会引发异常。 否则,将调用 valueToString 方法以生成其字符串表示形式,生成的字符串将写入标准输出流。

另请参阅

适用于

Write(Single)

Source:
Console.cs
Source:
Console.cs
Source:
Console.cs

将指定单精度浮点值的文本表示形式写入标准输出流。

public static void Write (float value);

参数

value
Single

要写入的值。

例外

出现 I/O 错误。

示例

以下示例演示了如何使用 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 (var item in fields)
            {
                if (isFirstField)
                    isFirstField = false;
                else
                    Console.Write(',');

                // If the field represents a boolean, replace with a numeric representation.
                bool itemBool;
                if (Boolean.TryParse(item, out itemBool))
                    Console.Write(Convert.ToByte(itemBool));
                else
                    Console.Write(item);
            }
            Console.WriteLine();
        }
    }
}

注解

value 的文本表示形式是通过调用 Single.ToString 方法生成的。

另请参阅

适用于

Write(Char)

Source:
Console.cs
Source:
Console.cs
Source:
Console.cs

将指定的 Unicode 字符值写入标准输出流。

public static void Write (char value);

参数

value
Char

要写入的值。

例外

出现 I/O 错误。

示例

以下示例演示了如何使用 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 (var item in fields)
            {
                if (isFirstField)
                    isFirstField = false;
                else
                    Console.Write(',');

                // If the field represents a boolean, replace with a numeric representation.
                bool itemBool;
                if (Boolean.TryParse(item, out itemBool))
                    Console.Write(Convert.ToByte(itemBool));
                else
                    Console.Write(item);
            }
            Console.WriteLine();
        }
    }
}

另请参阅

适用于

Write(Char[])

Source:
Console.cs
Source:
Console.cs
Source:
Console.cs

将指定的 Unicode 字符数组写入标准输出流。

public static void Write (char[]? buffer);
public static void Write (char[] buffer);

参数

buffer
Char[]

Unicode 字符数组。

例外

出现 I/O 错误。

另请参阅

适用于

Write(Boolean)

Source:
Console.cs
Source:
Console.cs
Source:
Console.cs

将指定布尔值的文本表示形式写入标准输出流。

public static void Write (bool value);

参数

value
Boolean

要写入的值。

例外

出现 I/O 错误。

示例

以下示例演示了如何使用 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 (var item in fields)
            {
                if (isFirstField)
                    isFirstField = false;
                else
                    Console.Write(',');

                // If the field represents a boolean, replace with a numeric representation.
                bool itemBool;
                if (Boolean.TryParse(item, out itemBool))
                    Console.Write(Convert.ToByte(itemBool));
                else
                    Console.Write(item);
            }
            Console.WriteLine();
        }
    }
}

注解

value 的文本表示形式是通过调用 Boolean.ToString生成的,后者输出 Boolean.TrueStringBoolean.FalseString

另请参阅

适用于

Write(Double)

Source:
Console.cs
Source:
Console.cs
Source:
Console.cs

将指定双精度浮点值的文本表示形式写入标准输出流。

public static void Write (double value);

参数

value
Double

要写入的值。

例外

出现 I/O 错误。

示例

以下示例演示了如何使用 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 (var item in fields)
            {
                if (isFirstField)
                    isFirstField = false;
                else
                    Console.Write(',');

                // If the field represents a boolean, replace with a numeric representation.
                bool itemBool;
                if (Boolean.TryParse(item, out itemBool))
                    Console.Write(Convert.ToByte(itemBool));
                else
                    Console.Write(item);
            }
            Console.WriteLine();
        }
    }
}

注解

value 的文本表示形式是通过调用 Double.ToString 方法生成的。

另请参阅

适用于

Write(Int32)

Source:
Console.cs
Source:
Console.cs
Source:
Console.cs

将指定 32 位有符号整数值的文本表示形式写入标准输出流。

public static void Write (int value);

参数

value
Int32

要写入的值。

例外

出现 I/O 错误。

示例

以下示例演示了如何使用 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 (var item in fields)
            {
                if (isFirstField)
                    isFirstField = false;
                else
                    Console.Write(',');

                // If the field represents a boolean, replace with a numeric representation.
                bool itemBool;
                if (Boolean.TryParse(item, out itemBool))
                    Console.Write(Convert.ToByte(itemBool));
                else
                    Console.Write(item);
            }
            Console.WriteLine();
        }
    }
}

注解

value 的文本表示形式是通过调用 Int32.ToString 方法生成的。

另请参阅

适用于

Write(Int64)

Source:
Console.cs
Source:
Console.cs
Source:
Console.cs

将指定 64 位带符号整数值的文本表示形式写入标准输出流。

public static void Write (long value);

参数

value
Int64

要写入的值。

例外

出现 I/O 错误。

示例

以下示例演示了如何使用 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 (var item in fields)
            {
                if (isFirstField)
                    isFirstField = false;
                else
                    Console.Write(',');

                // If the field represents a boolean, replace with a numeric representation.
                bool itemBool;
                if (Boolean.TryParse(item, out itemBool))
                    Console.Write(Convert.ToByte(itemBool));
                else
                    Console.Write(item);
            }
            Console.WriteLine();
        }
    }
}

注解

value 的文本表示形式是通过调用 Int64.ToString 方法生成的。

另请参阅

适用于

Write(Decimal)

Source:
Console.cs
Source:
Console.cs
Source:
Console.cs

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

public static void Write (decimal value);

参数

value
Decimal

要写入的值。

例外

出现 I/O 错误。

示例

以下示例演示了如何使用 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 (var item in fields)
            {
                if (isFirstField)
                    isFirstField = false;
                else
                    Console.Write(',');

                // If the field represents a boolean, replace with a numeric representation.
                bool itemBool;
                if (Boolean.TryParse(item, out itemBool))
                    Console.Write(Convert.ToByte(itemBool));
                else
                    Console.Write(item);
            }
            Console.WriteLine();
        }
    }
}

注解

value 的文本表示形式是通过调用 Decimal.ToString生成的。

另请参阅

适用于