Console.WriteLine 方法

定義

將指定的數據,後面接著目前的行終止符寫入標準輸出數據流。

多載

WriteLine(String, Object, Object)

使用指定的格式資訊,將指定物件的文字表示,後面接著目前的行終止符寫入標準輸出數據流。

WriteLine(String)

將指定的字串值,後面接著目前的行終止符寫入標準輸出數據流。

WriteLine(Char[], Int32, Int32)

將 Unicode 字元的指定子陣列,後面接著目前的行終止符寫入標準輸出數據流。

WriteLine(String, ReadOnlySpan<Object>)

使用指定的格式資訊,將指定之物件範圍的文字表示,後面接著目前的行終止符寫入標準輸出數據流。

WriteLine(String, Object[])

使用指定的格式資訊,將指定之 物件數位的文字表示,後面接著目前的行終止符寫入標準輸出數據流。

WriteLine(String, Object)

使用指定的格式資訊,將指定物件的文字表示,後面接著目前的行終止符寫入標準輸出數據流。

WriteLine(UInt64)

將指定之 64 位無符號整數值的文字表示,後面接著目前的行終止符寫入標準輸出數據流。

WriteLine(UInt32)

將指定之 32 位無符號整數值的文字表示,後面接著目前的行終止符寫入標準輸出數據流。

WriteLine(Single)

將指定之單精度浮點值的文字表示,後面接著目前的行終止符寫入標準輸出數據流。

WriteLine(Double)

將指定之雙精確度浮點值的文字表示,後面接著目前的行終止符寫入標準輸出數據流。

WriteLine(Int64)

將指定之64位帶正負號整數值的文字表示,後面接著目前的行終止符寫入標準輸出數據流。

WriteLine(Int32)

將指定之 32 位帶正負號整數值的文字表示,後面接著目前的行終止符寫入標準輸出數據流。

WriteLine(Decimal)

將指定之 Decimal 值的文字表示,後面接著目前的行終止符寫入標準輸出數據流。

WriteLine(Char[])

將指定的 Unicode 字元陣列,後面接著目前的行終止符寫入標準輸出數據流。

WriteLine(Char)

將指定的 Unicode 字元,後面接著目前的行終止符,值寫入標準輸出數據流。

WriteLine(Boolean)

將指定布爾值的文字表示,後面接著目前的行終止符寫入標準輸出數據流。

WriteLine()

將目前的行終止符寫入標準輸出數據流。

WriteLine(String, Object, Object, Object)

使用指定的格式資訊,將指定物件的文字表示,後面接著目前的行終止符寫入標準輸出數據流。

WriteLine(Object)

將指定物件的文字表示,後面接著目前的行終止符寫入標準輸出數據流。

WriteLine(String, Object, Object, Object, Object)

使用指定的格式資訊,將指定物件和可變長度參數清單的文字表示,後面接著目前的行終止符寫入標準輸出數據流。

備註

默認行終止符是字串,其值為歸位字元,後面接著 C# 中的換行字元(C# 中的“\r\n”,或 Visual Basic 中的 vbCrLf)。 您可以將 Out 屬性的 TextWriter.NewLine 屬性設定為另一個字串,以變更行終止符。

WriteLine(String, Object, Object)

來源:
Console.cs
來源:
Console.cs
來源:
Console.cs

使用指定的格式資訊,將指定物件的文字表示,後面接著目前的行終止符寫入標準輸出數據流。

C#
public static void WriteLine (string format, object? arg0, object? arg1);
C#
public static void WriteLine (string format, object arg0, object arg1);

參數

format
String

複合格式字串。

arg0
Object

使用 format寫入的第一個物件。

arg1
Object

使用 format寫入的第二個 物件。

例外狀況

發生 I/O 錯誤。

format null

format 中的格式規格無效。

範例

下列範例示範數位、日期和列舉的標準格式規範。

C#
// 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

*/

下列範例是小費計算機,會計算 18% 小費,並使用 WriteLine 方法來顯示原始費用的金額、小費金額和總金額。 此範例是主控台應用程式,要求使用者以命令行參數的形式提供原始費用的數量。

C#
using System;

public class TipCalculator
{
    private const double tipRate = 0.18;
    public static void Main(string[] args)
    {
        double billTotal;
        if (args.Length == 0 || ! Double.TryParse(args[0], out billTotal))
        {
            Console.WriteLine("usage: TIPCALC total");
            return;
        }
        double tip = billTotal * tipRate;
        Console.WriteLine();
        Console.WriteLine($"Bill total:\t{billTotal,8:c}");
        Console.WriteLine($"Tip total/rate:\t{tip,8:c} ({tipRate:p1})");
        Console.WriteLine(("").PadRight(24, '-'));
        Console.WriteLine($"Grand total:\t{billTotal + tip,8:c}");
    }
}

/*
>tipcalc 52.23

Bill total:       $52.23
Tip total/rate:    $9.40 (18.0 %)
------------------------
Grand total:      $61.63
*/

備註

此方法會使用 .NET 的 複合格式設定功能,將物件的值轉換成其文字表示法,並將該表示法內嵌在字串中。 產生的字串會寫入輸出數據流。

format 參數包含零或多個文字執行,與零或多個索引佔位元混合,稱為格式項目,對應至此方法參數清單中的物件。 格式化程式會將每個格式專案取代為對應物件的文字表示。

格式項目的語法是 {索引[,對齊][:formatString]},指定強制索引、格式化文字的選擇性長度和對齊方式,以及管理對應物件值格式化方式的格式規範字元選擇性字元串。

.NET 提供廣泛的格式支援,如下列格式化主題中更詳細地說明。

如需行終止符的詳細資訊,請參閱不採用參數之 WriteLine 方法的一節。

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.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, 8, 9
.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, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1

WriteLine(String)

來源:
Console.cs
來源:
Console.cs
來源:
Console.cs

將指定的字串值,後面接著目前的行終止符寫入標準輸出數據流。

C#
public static void WriteLine (string? value);
C#
public static void WriteLine (string value);

參數

value
String

要寫入的值。

例外狀況

發生 I/O 錯誤。

範例

此範例會將行終止符從預設值\r\n「 或 vbCrLf 變更為 」\r\n\r\n或 vbCrLf + vbCrLf。 然後它會呼叫 WriteLine()WriteLine(String) 方法來顯示主控台的輸出。

C#
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.

備註

如果 value 是 null,則只會將行終止符寫入標準輸出數據流。

如需行終止符的詳細資訊,請參閱 WriteLine() 方法的一節。

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.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, 8, 9
.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, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1

WriteLine(Char[], Int32, Int32)

來源:
Console.cs
來源:
Console.cs
來源:
Console.cs

將 Unicode 字元的指定子陣列,後面接著目前的行終止符寫入標準輸出數據流。

C#
public static void WriteLine (char[] buffer, int index, int count);

參數

buffer
Char[]

Unicode 字元的陣列。

index
Int32

buffer中的起始位置。

count
Int32

要寫入的字元數。

例外狀況

buffer null

indexcount 小於零。

index 加上 count 指定不在 buffer內的位置。

發生 I/O 錯誤。

備註

這個方法會將從位置 indexbuffer 的字元寫入標準輸出數據流 count 字元。

如需行終止符的詳細資訊,請參閱不採用參數之 WriteLine 方法的一節。

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.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, 8, 9
.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, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1

WriteLine(String, ReadOnlySpan<Object>)

使用指定的格式資訊,將指定之物件範圍的文字表示,後面接著目前的行終止符寫入標準輸出數據流。

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

參數

format
String

複合格式字串。

arg
ReadOnlySpan<Object>

使用格式寫入的物件範圍。

適用於

.NET 9
產品 版本
.NET 9

WriteLine(String, Object[])

來源:
Console.cs
來源:
Console.cs
來源:
Console.cs

使用指定的格式資訊,將指定之 物件數位的文字表示,後面接著目前的行終止符寫入標準輸出數據流。

C#
public static void WriteLine (string format, params object?[]? arg);
C#
public static void WriteLine (string format, params object[] arg);

參數

format
String

複合格式字串。

arg
Object[]

使用 format寫入的物件陣列。

例外狀況

發生 I/O 錯誤。

formatargnull

format 中的格式規格無效。

範例

下列範例示範數位、日期和列舉的標準格式規範。

C#
// 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

*/

下列範例是小費計算機,會計算 18% 小費,並使用 WriteLine 方法來顯示原始費用的金額、小費金額和總金額。 此範例是主控台應用程式,要求使用者以命令行參數的形式提供原始費用的數量。

C#
using System;

public class TipCalculator
{
    private const double tipRate = 0.18;
    public static void Main(string[] args)
    {
        double billTotal;
        if (args.Length == 0 || ! Double.TryParse(args[0], out billTotal))
        {
            Console.WriteLine("usage: TIPCALC total");
            return;
        }
        double tip = billTotal * tipRate;
        Console.WriteLine();
        Console.WriteLine($"Bill total:\t{billTotal,8:c}");
        Console.WriteLine($"Tip total/rate:\t{tip,8:c} ({tipRate:p1})");
        Console.WriteLine(("").PadRight(24, '-'));
        Console.WriteLine($"Grand total:\t{billTotal + tip,8:c}");
    }
}

/*
>tipcalc 52.23

Bill total:       $52.23
Tip total/rate:    $9.40 (18.0 %)
------------------------
Grand total:      $61.63
*/

備註

此方法會使用 .NET 的 複合格式設定功能,將物件的值轉換成其文字表示法,並將該表示法內嵌在字串中。 產生的字串會寫入輸出數據流。

format 參數包含零或多個文字執行,與零或多個索引佔位元混合,稱為格式項目,對應至此方法參數清單中的物件。 格式化程式會將每個格式專案取代為對應物件的文字表示。

格式項目的語法是 {索引[,對齊][:formatString]},指定強制索引、格式化文字的選擇性長度和對齊方式,以及管理對應物件值格式化方式的格式規範字元選擇性字元串。

.NET 提供廣泛的格式支援,如下列格式化主題中更詳細地說明。

如需行終止符的詳細資訊,請參閱不採用參數之 WriteLine 方法的一節。

給呼叫者的注意事項

C++程式代碼不會呼叫這個方法。 C++編譯程式會解析對 System.Console.WriteLine 的呼叫,其中包含字串和四個以上物件參數的清單,做為對 WriteLine(String, Object, Object, Object, Object)的呼叫。 它會解析 System.Console.WriteLine 的呼叫,其中包含字串和物件數位,以呼叫 WriteLine(String, Object)

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.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, 8, 9
.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, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1

WriteLine(String, Object)

來源:
Console.cs
來源:
Console.cs
來源:
Console.cs

使用指定的格式資訊,將指定物件的文字表示,後面接著目前的行終止符寫入標準輸出數據流。

C#
public static void WriteLine (string format, object? arg0);
C#
public static void WriteLine (string format, object arg0);

參數

format
String

複合格式字串。

arg0
Object

使用 format寫入的物件。

例外狀況

發生 I/O 錯誤。

format null

format 中的格式規格無效。

範例

下列範例會呼叫 WriteLine(String, Object) 方法來顯示五個隨機產生的 Boolean 值。

C#
Random rnd = new Random();
// Generate five random Boolean values.
for (int ctr = 1; ctr <= 5; ctr++) {
   bool bln = rnd.Next(0, 2) == 1;
   Console.WriteLine($"True or False: {bln}");
}

// The example displays an output similar to the following:
//       True or False: False
//       True or False: True
//       True or False: False
//       True or False: False
//       True or False: True

下列範例會呼叫 WriteLine(String, Object) 方法來顯示目前的日期。 請注意,format 自變數中的格式專案會使用 「D」 標準日期和時間格式字串,以目前文化特性的長日期格式顯示日期。

C#
using System;

public class Example
{
   public static void Main()
   {
      Console.WriteLine("Today's date: {0:D}", DateTime.Now);
   }
}
// The example displays output like the following:
//       Today's date: Monday, April 1, 2019

備註

此方法會使用 .NET 的 複合格式設定功能,將物件的值轉換成其文字表示法,並將該表示法內嵌在字串中。 產生的字串會寫入輸出數據流。

format 參數包含零或多個文字執行,與零或多個索引佔位元混合,稱為格式項目,對應至此方法參數清單中的物件。 格式化程式會將每個格式專案取代為對應物件的文字表示。

格式項目的語法是 {索引[,對齊][:formatString]},指定強制索引、格式化文字的選擇性長度和對齊方式,以及管理對應物件值格式化方式的格式規範字元選擇性字元串。

.NET 提供廣泛的格式支援,如下列格式化主題中更詳細地說明。

如需行終止符的詳細資訊,請參閱不採用參數之 WriteLine 方法的一節。

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.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, 8, 9
.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, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1

WriteLine(UInt64)

來源:
Console.cs
來源:
Console.cs
來源:
Console.cs

重要

此 API 不符合 CLS 規範。

將指定之 64 位無符號整數值的文字表示,後面接著目前的行終止符寫入標準輸出數據流。

C#
[System.CLSCompliant(false)]
public static void WriteLine (ulong value);

參數

value
UInt64

要寫入的值。

屬性

例外狀況

發生 I/O 錯誤。

範例

下列範例是小費計算機,會計算 18% 小費,並使用 WriteLine 方法來顯示原始費用的金額、小費金額和總金額。 此範例是主控台應用程式,要求使用者以命令行參數的形式提供原始費用的數量。

C#
using System;

public class TipCalculator
{
    private const double tipRate = 0.18;
    public static void Main(string[] args)
    {
        double billTotal;
        if (args.Length == 0 || ! Double.TryParse(args[0], out billTotal))
        {
            Console.WriteLine("usage: TIPCALC total");
            return;
        }
        double tip = billTotal * tipRate;
        Console.WriteLine();
        Console.WriteLine($"Bill total:\t{billTotal,8:c}");
        Console.WriteLine($"Tip total/rate:\t{tip,8:c} ({tipRate:p1})");
        Console.WriteLine(("").PadRight(24, '-'));
        Console.WriteLine($"Grand total:\t{billTotal + tip,8:c}");
    }
}

/*
>tipcalc 52.23

Bill total:       $52.23
Tip total/rate:    $9.40 (18.0 %)
------------------------
Grand total:      $61.63
*/

備註

呼叫 UInt64.ToString 方法會產生 value 的文字表示。

如需行終止符的詳細資訊,請參閱不採用參數之 WriteLine 方法的一節。

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.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, 8, 9
.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, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1

WriteLine(UInt32)

來源:
Console.cs
來源:
Console.cs
來源:
Console.cs

重要

此 API 不符合 CLS 規範。

將指定之 32 位無符號整數值的文字表示,後面接著目前的行終止符寫入標準輸出數據流。

C#
[System.CLSCompliant(false)]
public static void WriteLine (uint value);

參數

value
UInt32

要寫入的值。

屬性

例外狀況

發生 I/O 錯誤。

範例

下列範例是小費計算機,會計算 18% 小費,並使用 WriteLine 方法來顯示原始費用的金額、小費金額和總金額。 此範例是主控台應用程式,要求使用者以命令行參數的形式提供原始費用的數量。

C#
using System;

public class TipCalculator
{
    private const double tipRate = 0.18;
    public static void Main(string[] args)
    {
        double billTotal;
        if (args.Length == 0 || ! Double.TryParse(args[0], out billTotal))
        {
            Console.WriteLine("usage: TIPCALC total");
            return;
        }
        double tip = billTotal * tipRate;
        Console.WriteLine();
        Console.WriteLine($"Bill total:\t{billTotal,8:c}");
        Console.WriteLine($"Tip total/rate:\t{tip,8:c} ({tipRate:p1})");
        Console.WriteLine(("").PadRight(24, '-'));
        Console.WriteLine($"Grand total:\t{billTotal + tip,8:c}");
    }
}

/*
>tipcalc 52.23

Bill total:       $52.23
Tip total/rate:    $9.40 (18.0 %)
------------------------
Grand total:      $61.63
*/

備註

呼叫 UInt32.ToString 方法會產生 value 的文字表示。

如需行終止符的詳細資訊,請參閱不採用參數之 WriteLine 方法的一節。

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.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, 8, 9
.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, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1

WriteLine(Single)

來源:
Console.cs
來源:
Console.cs
來源:
Console.cs

將指定之單精度浮點值的文字表示,後面接著目前的行終止符寫入標準輸出數據流。

C#
public static void WriteLine (float value);

參數

value
Single

要寫入的值。

例外狀況

發生 I/O 錯誤。

範例

下列範例是小費計算機,會計算 18% 小費,並使用 WriteLine 方法來顯示原始費用的金額、小費金額和總金額。 此範例是主控台應用程式,要求使用者以命令行參數的形式提供原始費用的數量。

C#
using System;

public class TipCalculator
{
    private const double tipRate = 0.18;
    public static void Main(string[] args)
    {
        double billTotal;
        if (args.Length == 0 || ! Double.TryParse(args[0], out billTotal))
        {
            Console.WriteLine("usage: TIPCALC total");
            return;
        }
        double tip = billTotal * tipRate;
        Console.WriteLine();
        Console.WriteLine($"Bill total:\t{billTotal,8:c}");
        Console.WriteLine($"Tip total/rate:\t{tip,8:c} ({tipRate:p1})");
        Console.WriteLine(("").PadRight(24, '-'));
        Console.WriteLine($"Grand total:\t{billTotal + tip,8:c}");
    }
}

/*
>tipcalc 52.23

Bill total:       $52.23
Tip total/rate:    $9.40 (18.0 %)
------------------------
Grand total:      $61.63
*/

備註

呼叫 Single.ToString 方法會產生 value 的文字表示。

如需行終止符的詳細資訊,請參閱不採用參數之 WriteLine 方法的一節。

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.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, 8, 9
.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, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1

WriteLine(Double)

來源:
Console.cs
來源:
Console.cs
來源:
Console.cs

將指定之雙精確度浮點值的文字表示,後面接著目前的行終止符寫入標準輸出數據流。

C#
public static void WriteLine (double value);

參數

value
Double

要寫入的值。

例外狀況

發生 I/O 錯誤。

範例

下列範例是小費計算機,會計算 18% 小費,並使用 WriteLine 方法來顯示原始費用的金額、小費金額和總金額。 此範例是主控台應用程式,要求使用者以命令行參數的形式提供原始費用的數量。

C#
using System;

public class TipCalculator
{
    private const double tipRate = 0.18;
    public static void Main(string[] args)
    {
        double billTotal;
        if (args.Length == 0 || ! Double.TryParse(args[0], out billTotal))
        {
            Console.WriteLine("usage: TIPCALC total");
            return;
        }
        double tip = billTotal * tipRate;
        Console.WriteLine();
        Console.WriteLine($"Bill total:\t{billTotal,8:c}");
        Console.WriteLine($"Tip total/rate:\t{tip,8:c} ({tipRate:p1})");
        Console.WriteLine(("").PadRight(24, '-'));
        Console.WriteLine($"Grand total:\t{billTotal + tip,8:c}");
    }
}

/*
>tipcalc 52.23

Bill total:       $52.23
Tip total/rate:    $9.40 (18.0 %)
------------------------
Grand total:      $61.63
*/

備註

呼叫 Double.ToString 方法會產生 value 的文字表示。

如需行終止符的詳細資訊,請參閱不採用參數之 WriteLine 方法的一節。

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.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, 8, 9
.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, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1

WriteLine(Int64)

來源:
Console.cs
來源:
Console.cs
來源:
Console.cs

將指定之64位帶正負號整數值的文字表示,後面接著目前的行終止符寫入標準輸出數據流。

C#
public static void WriteLine (long value);

參數

value
Int64

要寫入的值。

例外狀況

發生 I/O 錯誤。

範例

下列範例是小費計算機,會計算 18% 小費,並使用 WriteLine 方法來顯示原始費用的金額、小費金額和總金額。 此範例是主控台應用程式,要求使用者以命令行參數的形式提供原始費用的數量。

C#
using System;

public class TipCalculator
{
    private const double tipRate = 0.18;
    public static void Main(string[] args)
    {
        double billTotal;
        if (args.Length == 0 || ! Double.TryParse(args[0], out billTotal))
        {
            Console.WriteLine("usage: TIPCALC total");
            return;
        }
        double tip = billTotal * tipRate;
        Console.WriteLine();
        Console.WriteLine($"Bill total:\t{billTotal,8:c}");
        Console.WriteLine($"Tip total/rate:\t{tip,8:c} ({tipRate:p1})");
        Console.WriteLine(("").PadRight(24, '-'));
        Console.WriteLine($"Grand total:\t{billTotal + tip,8:c}");
    }
}

/*
>tipcalc 52.23

Bill total:       $52.23
Tip total/rate:    $9.40 (18.0 %)
------------------------
Grand total:      $61.63
*/

備註

呼叫 Int64.ToString 方法會產生 value 的文字表示。

如需行終止符的詳細資訊,請參閱不採用參數之 WriteLine 方法的一節。

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.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, 8, 9
.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, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1

WriteLine(Int32)

來源:
Console.cs
來源:
Console.cs
來源:
Console.cs

將指定之 32 位帶正負號整數值的文字表示,後面接著目前的行終止符寫入標準輸出數據流。

C#
public static void WriteLine (int value);

參數

value
Int32

要寫入的值。

例外狀況

發生 I/O 錯誤。

範例

下列範例是小費計算機,會計算 18% 小費,並使用 WriteLine 方法來顯示原始費用的金額、小費金額和總金額。 此範例是主控台應用程式,要求使用者以命令行參數的形式提供原始費用的數量。

C#
using System;

public class TipCalculator
{
    private const double tipRate = 0.18;
    public static void Main(string[] args)
    {
        double billTotal;
        if (args.Length == 0 || ! Double.TryParse(args[0], out billTotal))
        {
            Console.WriteLine("usage: TIPCALC total");
            return;
        }
        double tip = billTotal * tipRate;
        Console.WriteLine();
        Console.WriteLine($"Bill total:\t{billTotal,8:c}");
        Console.WriteLine($"Tip total/rate:\t{tip,8:c} ({tipRate:p1})");
        Console.WriteLine(("").PadRight(24, '-'));
        Console.WriteLine($"Grand total:\t{billTotal + tip,8:c}");
    }
}

/*
>tipcalc 52.23

Bill total:       $52.23
Tip total/rate:    $9.40 (18.0 %)
------------------------
Grand total:      $61.63
*/

備註

呼叫 Int32.ToString 方法會產生 value 的文字表示。

如需行終止符的詳細資訊,請參閱不採用參數之 WriteLine 方法的一節。

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.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, 8, 9
.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, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1

WriteLine(Decimal)

來源:
Console.cs
來源:
Console.cs
來源:
Console.cs

將指定之 Decimal 值的文字表示,後面接著目前的行終止符寫入標準輸出數據流。

C#
public static void WriteLine (decimal value);

參數

value
Decimal

要寫入的值。

例外狀況

發生 I/O 錯誤。

範例

下列範例是小費計算機,會計算 18% 小費,並使用 WriteLine 方法來顯示原始費用的金額、小費金額和總金額。 此範例是主控台應用程式,要求使用者以命令行參數的形式提供原始費用的數量。

C#
using System;

public class TipCalculator
{
    private const double tipRate = 0.18;
    public static void Main(string[] args)
    {
        double billTotal;
        if (args.Length == 0 || ! Double.TryParse(args[0], out billTotal))
        {
            Console.WriteLine("usage: TIPCALC total");
            return;
        }
        double tip = billTotal * tipRate;
        Console.WriteLine();
        Console.WriteLine($"Bill total:\t{billTotal,8:c}");
        Console.WriteLine($"Tip total/rate:\t{tip,8:c} ({tipRate:p1})");
        Console.WriteLine(("").PadRight(24, '-'));
        Console.WriteLine($"Grand total:\t{billTotal + tip,8:c}");
    }
}

/*
>tipcalc 52.23

Bill total:       $52.23
Tip total/rate:    $9.40 (18.0 %)
------------------------
Grand total:      $61.63
*/

備註

呼叫 Decimal.ToString 方法會產生 value 的文字表示。

如需行終止符的詳細資訊,請參閱不採用參數之 WriteLine 方法的一節。

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.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, 8, 9
.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, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1

WriteLine(Char[])

來源:
Console.cs
來源:
Console.cs
來源:
Console.cs

將指定的 Unicode 字元陣列,後面接著目前的行終止符寫入標準輸出數據流。

C#
public static void WriteLine (char[]? buffer);
C#
public static void WriteLine (char[] buffer);

參數

buffer
Char[]

Unicode 字元陣列。

例外狀況

發生 I/O 錯誤。

備註

如需行終止符的詳細資訊,請參閱不採用參數之 WriteLine 方法的一節。

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.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, 8, 9
.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, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1

WriteLine(Char)

來源:
Console.cs
來源:
Console.cs
來源:
Console.cs

將指定的 Unicode 字元,後面接著目前的行終止符,值寫入標準輸出數據流。

C#
public static void WriteLine (char value);

參數

value
Char

要寫入的值。

例外狀況

發生 I/O 錯誤。

範例

下列範例是小費計算機,會計算 18% 小費,並使用 WriteLine 方法來顯示原始費用的金額、小費金額和總金額。 此範例是主控台應用程式,要求使用者以命令行參數的形式提供原始費用的數量。

C#
using System;

public class TipCalculator
{
    private const double tipRate = 0.18;
    public static void Main(string[] args)
    {
        double billTotal;
        if (args.Length == 0 || ! Double.TryParse(args[0], out billTotal))
        {
            Console.WriteLine("usage: TIPCALC total");
            return;
        }
        double tip = billTotal * tipRate;
        Console.WriteLine();
        Console.WriteLine($"Bill total:\t{billTotal,8:c}");
        Console.WriteLine($"Tip total/rate:\t{tip,8:c} ({tipRate:p1})");
        Console.WriteLine(("").PadRight(24, '-'));
        Console.WriteLine($"Grand total:\t{billTotal + tip,8:c}");
    }
}

/*
>tipcalc 52.23

Bill total:       $52.23
Tip total/rate:    $9.40 (18.0 %)
------------------------
Grand total:      $61.63
*/

備註

如需行終止符的詳細資訊,請參閱不採用參數之 WriteLine 方法的一節。

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.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, 8, 9
.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, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1

WriteLine(Boolean)

來源:
Console.cs
來源:
Console.cs
來源:
Console.cs

將指定布爾值的文字表示,後面接著目前的行終止符寫入標準輸出數據流。

C#
public static void WriteLine (bool value);

參數

value
Boolean

要寫入的值。

例外狀況

發生 I/O 錯誤。

範例

下列範例會產生十個隨機整數,並使用 Console.WriteLine(Boolean) 方法來指出它們是否為偶數。

C#
// Assign 10 random integers to an array.
Random rnd = new Random();
int[] numbers = new int[10];
for (int ctr = 0; ctr <= numbers.GetUpperBound(0); ctr++)
   numbers[ctr] = rnd.Next();

// Determine whether the numbers are even or odd.
foreach (var number in numbers) {
   bool even = (number % 2 == 0);
   Console.WriteLine("Is {0} even:", number);
   Console.WriteLine(even);
   Console.WriteLine();
}

備註

呼叫 Boolean.ToString 方法會產生 value 的文字表示。

如需行終止符的詳細資訊,請參閱不採用參數之 WriteLine 方法的一節。

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.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, 8, 9
.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, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1

WriteLine()

來源:
Console.cs
來源:
Console.cs
來源:
Console.cs

將目前的行終止符寫入標準輸出數據流。

C#
public static void WriteLine ();

例外狀況

發生 I/O 錯誤。

範例

此範例會將行終止符從預設值\r\n「 或 vbCrLf 變更為 」\r\n\r\n或 vbCrLf + vbCrLf。 然後它會呼叫 WriteLine()WriteLine(String) 方法來顯示主控台的輸出。

C#
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.

備註

默認行終止符是字串,其值為歸位字元,後面接著 C# 中的換行字元(C# 中的“\r\n”,或 Visual Basic 中的 vbCrLf)。 您可以將 Out 屬性的 TextWriter.NewLine 屬性設定為另一個字串,以變更行終止符。 此範例提供圖例。

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.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, 8, 9
.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, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1

WriteLine(String, Object, Object, Object)

來源:
Console.cs
來源:
Console.cs
來源:
Console.cs

使用指定的格式資訊,將指定物件的文字表示,後面接著目前的行終止符寫入標準輸出數據流。

C#
public static void WriteLine (string format, object? arg0, object? arg1, object? arg2);
C#
public static void WriteLine (string format, object arg0, object arg1, object arg2);

參數

format
String

複合格式字串。

arg0
Object

使用 format寫入的第一個物件。

arg1
Object

使用 format寫入的第二個 物件。

arg2
Object

使用 format寫入的第三個物件。

例外狀況

發生 I/O 錯誤。

format null

format 中的格式規格無效。

範例

下列範例示範數位、日期和列舉的標準格式規範。

C#
// 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

*/

下列範例是小費計算機,會計算 18% 小費,並使用 WriteLine 方法來顯示原始費用的金額、小費金額和總金額。 此範例是主控台應用程式,要求使用者以命令行參數的形式提供原始費用的數量。

C#
using System;

public class TipCalculator
{
    private const double tipRate = 0.18;
    public static void Main(string[] args)
    {
        double billTotal;
        if (args.Length == 0 || ! Double.TryParse(args[0], out billTotal))
        {
            Console.WriteLine("usage: TIPCALC total");
            return;
        }
        double tip = billTotal * tipRate;
        Console.WriteLine();
        Console.WriteLine($"Bill total:\t{billTotal,8:c}");
        Console.WriteLine($"Tip total/rate:\t{tip,8:c} ({tipRate:p1})");
        Console.WriteLine(("").PadRight(24, '-'));
        Console.WriteLine($"Grand total:\t{billTotal + tip,8:c}");
    }
}

/*
>tipcalc 52.23

Bill total:       $52.23
Tip total/rate:    $9.40 (18.0 %)
------------------------
Grand total:      $61.63
*/

備註

此方法會使用 .NET 的 複合格式設定功能,將物件的值轉換成其文字表示法,並將該表示法內嵌在字串中。 產生的字串會寫入輸出數據流。

format 參數包含零或多個文字執行,與零或多個索引佔位元混合,稱為格式項目,對應至此方法參數清單中的物件。 格式化程式會將每個格式專案取代為對應物件的文字表示。

格式項目的語法是 {索引[,對齊][:formatString]},指定強制索引、格式化文字的選擇性長度和對齊方式,以及管理對應物件值格式化方式的格式規範字元選擇性字元串。

.NET 提供廣泛的格式支援,如下列格式化主題中更詳細地說明。

如需行終止符的詳細資訊,請參閱不採用參數之 WriteLine 方法的一節。

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.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, 8, 9
.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, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1

WriteLine(Object)

來源:
Console.cs
來源:
Console.cs
來源:
Console.cs

將指定物件的文字表示,後面接著目前的行終止符寫入標準輸出數據流。

C#
public static void WriteLine (object? value);
C#
public static void WriteLine (object value);

參數

value
Object

要寫入的值。

例外狀況

發生 I/O 錯誤。

範例

下列範例會使用 WriteLine(Object) 方法,將物件陣列中的每個值顯示給主控台。

C#
Object[] values = { true, 12.632, 17908, "stringValue",
                           'a', 16907.32m };
foreach (var value in values)
   Console.WriteLine(value);

// The example displays the following output:
//    True
//    12.632
//    17908
//    stringValue
//    a
//    16907.32

備註

如果 valuenull,則只會寫入行終止符。 否則,會呼叫 valueToString 方法來產生其字串表示法,並將產生的字串寫入標準輸出數據流。

如需行終止符的詳細資訊,請參閱不採用參數之 WriteLine 方法的一節。

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.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, 8, 9
.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, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1

WriteLine(String, Object, Object, Object, Object)

重要

此 API 不符合 CLS 規範。

使用指定的格式資訊,將指定物件和可變長度參數清單的文字表示,後面接著目前的行終止符寫入標準輸出數據流。

C#
[System.CLSCompliant(false)]
public static void WriteLine (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 中的格式規格無效。

範例

下列範例說明搭配 WriteLine(String, Object, Object, Object, Object) 方法使用變數自變數。 方法會使用複合格式字串和五個格式專案來呼叫。

C++
using namespace System;

int CountLetters(String^ value);
int CountWhitespace(String^ value);

void main()
{
   String^ value = "This is a test string.";
   
   
   Console::WriteLine("The string '{0}' consists of:" +
                      "{4}{1} characters{4}{2} letters{4}" +
                      "{3} white-space characters", 
                      value, value->Length, CountLetters(value), 
                      CountWhitespace(value), Environment::NewLine);
}

int CountLetters(String^ value)
{
   int nLetters = 0;
   for each (Char ch in value) {
      if (Char::IsLetter(ch))
         nLetters++;
   }
   return nLetters;
}

int CountWhitespace(String^ value)
{
   int nWhitespace = 0;
   for each (Char ch in value) {
      if (Char::IsWhiteSpace(ch))
         nWhitespace++;
   }
   return nWhitespace;
}
// The example displays the following output:
//    The string 'This is a test string.' consists of:
//    22 characters
//    17 letters
//    4 white-space characters

備註

備註

此 API 不符合 CLS 標準。 符合 CLS 標準的替代方案 Console.WriteLine(String, Object[])。 C# 和 Visual Basic 編譯程式會自動解析對此方法的呼叫,以呼叫 Console.WriteLine(String, Object[])

此方法會使用 .NET 的 複合格式設定功能,將物件的值轉換成其文字表示法,並將該表示法內嵌在字串中。 產生的字串會寫入輸出數據流。

format 參數包含零或多個文字執行,與零或多個索引佔位元混合,稱為格式項目,對應至此方法參數清單中的物件。 格式化程式會將每個格式專案取代為對應物件的文字表示。

格式項目的語法是 {索引[,對齊][:formatString]},指定強制索引、格式化文字的選擇性長度和對齊方式,以及管理對應物件值格式化方式的格式規範字元選擇性字元串。

.NET 提供廣泛的格式支援,如下列格式化主題中更詳細地說明。

如需行終止符的詳細資訊,請參閱不採用參數之 WriteLine 方法的一節。

給呼叫者的注意事項

這個方法會以 vararg 關鍵詞標示,這表示它支援變數數目的參數。 方法可以從 Visual C++呼叫,但無法從 C# 或 Visual Basic 程式代碼呼叫。 C# 和 Visual Basic 編譯程式會將對 WriteLine(String, Object, Object, Object, Object) 的呼叫解析為對 WriteLine(String, Object[])的呼叫。

另請參閱

適用於

.NET Framework 4.8.1 和其他版本
產品 版本
.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, 4.8.1