Console.WriteLine Console.WriteLine Console.WriteLine Console.WriteLine Method

定义

将指定的数据(后跟当前行终止符)写入标准输出流。Writes the specified data, followed by the current line terminator, to the standard output stream.

重载

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

使用指定的格式信息,将指定对象的文本表示形式(后跟当前行终止符)写入标准输出流。Writes the text representation of the specified objects, followed by the current line terminator, to the standard output stream using the specified format information.

WriteLine(String) WriteLine(String) WriteLine(String) WriteLine(String)

将指定的字符串值(后跟当前行终止符)写入标准输出流。Writes the specified string value, followed by the current line terminator, to the standard output stream.

WriteLine(Char[], Int32, Int32) WriteLine(Char[], Int32, Int32) WriteLine(Char[], Int32, Int32) WriteLine(Char[], Int32, Int32)

将指定的 Unicode 字符子数组(后跟当前行终止符)写入标准输出流。Writes the specified subarray of Unicode characters, followed by the current line terminator, to the standard output stream.

WriteLine(String, Object[]) WriteLine(String, Object[]) WriteLine(String, Object[]) WriteLine(String, Object[])

使用指定的格式信息,将指定的对象数组(后跟当前行终止符)的文本表示形式写入标准输出流。Writes the text representation of the specified array of objects, followed by the current line terminator, to the standard output stream using the specified format information.

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

使用指定的格式信息,将指定对象(后跟当前行终止符)的文本表示形式写入标准输出流。Writes the text representation of the specified object, followed by the current line terminator, to the standard output stream using the specified format information.

WriteLine(UInt64) WriteLine(UInt64) WriteLine(UInt64) WriteLine(UInt64)

将指定的 64 位无符号的整数值的文本表示(后跟当前行的结束符)写入标准输出流。Writes the text representation of the specified 64-bit unsigned integer value, followed by the current line terminator, to the standard output stream.

WriteLine(UInt32) WriteLine(UInt32) WriteLine(UInt32) WriteLine(UInt32)

将指定的 32 位无符号的整数值的文本表示(后跟当前行的结束符)写入标准输出流。Writes the text representation of the specified 32-bit unsigned integer value, followed by the current line terminator, to the standard output stream.

WriteLine(Single) WriteLine(Single) WriteLine(Single) WriteLine(Single)

将指定的单精度浮点值的文本表示形式(后跟当前行终止符)写入标准输出流。Writes the text representation of the specified single-precision floating-point value, followed by the current line terminator, to the standard output stream.

WriteLine(Decimal) WriteLine(Decimal) WriteLine(Decimal) WriteLine(Decimal)

将指定的 Decimal 值的文本表示形式(后跟当前行终止符)写入标准输出流。Writes the text representation of the specified Decimal value, followed by the current line terminator, to the standard output stream.

WriteLine(Int64) WriteLine(Int64) WriteLine(Int64) WriteLine(Int64)

将指定的 64 位有符号整数值的文本表示(后跟当前行的结束符)写入标准输出流。Writes the text representation of the specified 64-bit signed integer value, followed by the current line terminator, to the standard output stream.

WriteLine(Int32) WriteLine(Int32) WriteLine(Int32) WriteLine(Int32)

将指定的 32 位有符号整数值的文本表示(后跟当前行的结束符)写入标准输出流。Writes the text representation of the specified 32-bit signed integer value, followed by the current line terminator, to the standard output stream.

WriteLine(Double) WriteLine(Double) WriteLine(Double) WriteLine(Double)

将指定的双精度浮点值的文本表示形式(后跟当前行终止符)写入标准输出流。Writes the text representation of the specified double-precision floating-point value, followed by the current line terminator, to the standard output stream.

WriteLine(Char[]) WriteLine(Char[]) WriteLine(Char[]) WriteLine(Char[])

将指定的 Unicode 字符数组(后跟当前行终止符)写入标准输出流。Writes the specified array of Unicode characters, followed by the current line terminator, to the standard output stream.

WriteLine(Char) WriteLine(Char) WriteLine(Char) WriteLine(Char)

将指定的 Unicode 字符值(后跟当前行终止符)写入标准输出流。Writes the specified Unicode character, followed by the current line terminator, value to the standard output stream.

WriteLine(Boolean) WriteLine(Boolean) WriteLine(Boolean) WriteLine(Boolean)

将指定布尔值的文本表示形式(后跟当前行终止符)写入标准输出流。Writes the text representation of the specified Boolean value, followed by the current line terminator, to the standard output stream.

WriteLine() WriteLine() WriteLine() WriteLine()

将当前行终止符写入标准输出流。Writes the current line terminator to the standard output stream.

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

使用指定的格式信息,将指定对象的文本表示形式(后跟当前行终止符)写入标准输出流。Writes the text representation of the specified objects, followed by the current line terminator, to the standard output stream using the specified format information.

WriteLine(Object) WriteLine(Object) WriteLine(Object) WriteLine(Object)

将指定对象的文本表示形式(后跟当前行终止符)写入标准输出流。Writes the text representation of the specified object, followed by the current line terminator, to the standard output stream.

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

使用指定的格式信息,将指定的对象和可变长度参数列表(后跟当前行终止符)的文本表示形式写入标准输出流。Writes the text representation of the specified objects and variable-length parameter list, followed by the current line terminator, to the standard output stream using the specified format information.

注解

默认行终止符是其值是回车符后跟换行符的字符串 ("\r\n"在 C# 中,或vbCrLf在 Visual Basic 中)。The default line terminator is a string whose value is a carriage return followed by a line feed ("\r\n" in C#, or vbCrLf in Visual Basic). 您可以通过设置更改的行终止符TextWriter.NewLine属性的Out到另一个字符串属性。You can change the line terminator by setting the TextWriter.NewLine property of the Out property to another string.

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

使用指定的格式信息,将指定对象的文本表示形式(后跟当前行终止符)写入标准输出流。Writes the text representation of the specified objects, followed by the current line terminator, to the standard output stream using the specified format information.

public:
 static void WriteLine(System::String ^ format, System::Object ^ arg0, System::Object ^ arg1);
public static void WriteLine (string format, object arg0, object arg1);
static member WriteLine : string * obj * obj -> unit
Public Shared Sub WriteLine (format As String, arg0 As Object, arg1 As Object)

参数

format
String String String String

复合格式字符串。A composite format string.

arg0
Object Object Object Object

要使用 format 写入的第一个对象。The first object to write using format.

arg1
Object Object Object Object

要使用 format 写入的第二个对象。The second object to write using format.

异常

出现 I/O 错误。An I/O error occurred.

format 中的格式规范无效。The format specification in format is invalid.

示例

下面的示例演示数字、日期和枚举的标准格式说明符。The following example demonstrates the standard formatting specifiers for numbers, dates, and enumerations.

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

using namespace System;

public enum class Color {Yellow = 1, Blue, Green};

int main() 
{
    DateTime thisDate = DateTime::Now;
    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

*/
// 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

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

Imports System
Imports Microsoft.VisualBasic

Class Sample
   Public Enum Color
      Yellow = 1
      Blue = 2
      Green = 3
   End Enum 'Color
   Private Shared thisDate As DateTime = DateTime.Now
   
   Public Shared Sub 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}" & vbCrLf & _
                        "(D) Decimal:. . . . . . . . . {0:D}" & vbCrLf & _
                        "(E) Scientific: . . . . . . . {1:E}" & vbCrLf & _
                        "(F) Fixed point:. . . . . . . {1:F}" & vbCrLf & _
                        "(G) General:. . . . . . . . . {0:G}" & vbCrLf & _
                        "    (default):. . . . . . . . {0} (default = 'G')" & vbCrLf & _
                        "(N) Number: . . . . . . . . . {0:N}" & vbCrLf & _
                        "(P) Percent:. . . . . . . . . {1:P}" & vbCrLf & _
                        "(R) Round-trip: . . . . . . . {1:R}" & vbCrLf & _
                        "(X) Hexadecimal:. . . . . . . {0:X}" & vbCrLf, _
                        - 123, - 123.45F)

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

      ' Format a Color enumeration value in various ways.
      Console.WriteLine("Standard Enumeration Format Specifiers")
      Console.WriteLine("(G) General:. . . . . . . . . {0:G}" & vbCrLf & _
                        "    (default):. . . . . . . . {0} (default = 'G')" & vbCrLf & _
                        "(F) Flags:. . . . . . . . . . {0:F} (flags or integer)" & vbCrLf & _
                        "(D) Decimal number: . . . . . {0:D}" & vbCrLf & _
                        "(X) Hexadecimal:. . . . . . . {0:X}" & vbCrLf, _
                        Color.Green)
   End Sub 'Main
End Class 'Sample
'
'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方法来显示原始费用金额、 小费金额和总金额。The following example is a tip calculator that calculates an 18% tip and uses the WriteLine method to display the amount of the original charge, the amount of the tip, and the total amount. 该示例是要求用户提供的作为命令行参数的原始费用的控制台应用程序。The example is a console application that requires the user to supply the amount of the original charge as a command-line parameter.

using System;

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

/*
>tipcalc 52.23

Bill total:       $52.23
Tip total/rate:    $9.40 (18.0 %)
------------------------
Grand total:      $61.63
*/
Public Class TipCalculator
   Private Const tipRate As Double = 0.18
   
   Public Shared Sub Main()
      System.Environment.ExitCode = Calculator(System.Environment.GetCommandLineArgs())
   End Sub
   
   Public Shared Function Calculator(args() As String) As Integer
      Dim billTotal As Double
      If args.Length < 2 Then
         Console.WriteLine("usage: TIPCALC total")
         Return 1
      Else
         If Not Double.TryParse(args(1), billTotal) Then
            Console.WriteLine("usage: TIPCALC total")
            Return 1
         End If
         
         Dim tip As Double = billTotal * tipRate
         Console.WriteLine()
         Console.WriteLine("Bill total:{1}{0,8:c}", billTotal, vbTab)
         Console.WriteLine("Tip total/rate:{2}{0,8:c} ({1:p1})", tip, tipRate, vbTab)
         Console.WriteLine("".PadRight(24, "-"c))
         Console.WriteLine("Grand total:{1}{0,8:c}", billTotal + tip, vbTab)
         Return 0
      End If
   End Function 
End Class 

'Example Output:
'---------------
' >tipcalc 52.23
' 
' Bill total:       $52.23
' Tip total/rate:    $9.40 (18.0 %)
' ------------------------
' Grand total:      $61.63

注解

此方法使用复合格式设置功能的.NET Framework,才能将对象的值转换为其文本表示形式,在字符串中嵌入该表示形式。This method uses the composite formatting feature of the .NET Framework to convert the value of an object to its text representation and embed that representation in a string. 生成的字符串写入输出流。The resulting string is written to the output stream.

format参数由零个或多个文本序列与零个或多个索引占位符称为格式项,对应于此方法的参数列表中的对象。The format parameter consists of zero or more runs of text intermixed with zero or more indexed placeholders, called format items, that correspond to an object in the parameter list of this method. 格式设置过程将每个格式项替换相应的对象的值的文本表示形式。The formatting process replaces each format item with the text representation of the value of the corresponding object.

格式项的语法是{索引[,对齐] [:formatString]},它指定强制索引、 可选长度和格式的文本的对齐方式和可选的格式说明符字符用于控制如何设置相应的对象的值的格式字符串。The syntax of a format item is {index[,alignment][:formatString]}, which specifies a mandatory index, the optional length and alignment of the formatted text, and an optional string of format specifier characters that govern how the value of the corresponding object is formatted.

.NET Framework 提供了广泛的格式设置支持,其格式设置的以下主题中更详细地介绍。The .NET Framework provides extensive formatting support, which is described in greater detail in the following formatting topics.

有关行终止符的详细信息,请参阅备注部分的WriteLine不带参数的方法。For more information about the line terminator, see the Remarks section of the WriteLine method that takes no parameters.

安全性

UIPermission
用于修改安全顶级窗口和子窗口。for modifying safe top-level windows and subwindows. 关联的枚举: SafeTopLevelWindowsAssociated enumeration: SafeTopLevelWindows

另请参阅

WriteLine(String) WriteLine(String) WriteLine(String) WriteLine(String)

将指定的字符串值(后跟当前行终止符)写入标准输出流。Writes the specified string value, followed by the current line terminator, to the standard output stream.

public:
 static void WriteLine(System::String ^ value);
public static void WriteLine (string value);
static member WriteLine : string -> unit
Public Shared Sub WriteLine (value As String)

参数

value
String String String String

要写入的值。The value to write.

异常

出现 I/O 错误。An I/O error occurred.

示例

该示例将更改其默认值为"\r\n"行终止符或vbCrLf到"\r\n\r\n"或vbCrLf + vbCrLfThe example changes the line terminator from its default value of "\r\n" or vbCrLf to "\r\n\r\n" or vbCrLf + vbCrLf. 然后,它调用WriteLine()WriteLine(String)输出显示到控制台的方法。It then calls the WriteLine() and WriteLine(String) methods to display output to the console.

using namespace System;

void main()
{
   array<String^>^ lines = gcnew array<String^> { "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();
   for each (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();
   for each (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.
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.
Module Example
   Public Sub Main()
      Dim lines() As String = { "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()
      For Each line As String In lines
         Console.WriteLine(line)
      Next
      Console.WriteLine()
      
      ' Redefine the newline characters to double space.
      Console.Out.NewLine = vbCrLf + vbCrLf
      ' Output the lines using the new newline sequence.
      Console.WriteLine("With redefined new line characters:")
      Console.WriteLine()
      For Each line As String In lines
         Console.WriteLine(line)
      Next
   End Sub
End Module
' 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.

注解

如果值为null,只将行终止符写入标准输出流。If value is null, only the line terminator is written to the standard output stream.

有关行终止符的详细信息,请参阅备注部分的WriteLine()方法。For more information about the line terminator, see the Remarks section of the WriteLine() method.

安全性

UIPermission
用于修改安全顶级窗口和子窗口。for modifying safe top-level windows and subwindows. 关联的枚举: SafeTopLevelWindowsAssociated enumeration: SafeTopLevelWindows

另请参阅

WriteLine(Char[], Int32, Int32) WriteLine(Char[], Int32, Int32) WriteLine(Char[], Int32, Int32) WriteLine(Char[], Int32, Int32)

将指定的 Unicode 字符子数组(后跟当前行终止符)写入标准输出流。Writes the specified subarray of Unicode characters, followed by the current line terminator, to the standard output stream.

public:
 static void WriteLine(cli::array <char> ^ buffer, int index, int count);
public static void WriteLine (char[] buffer, int index, int count);
static member WriteLine : char[] * int * int -> unit
Public Shared Sub WriteLine (buffer As Char(), index As Integer, count As Integer)

参数

buffer
Char[]

Unicode 字符的数组。An array of Unicode characters.

index
Int32 Int32 Int32 Int32

buffer 中的起始位置。The starting position in buffer.

count
Int32 Int32 Int32 Int32

要写入的字符数。The number of characters to write.

异常

indexcount 指定不在 buffer 内的位置。index plus count specify a position that is not within buffer.

出现 I/O 错误。An I/O error occurred.

注解

此方法写入count字符位置开始indexbuffer到标准输出流。This method writes count characters starting at position index of buffer to the standard output stream.

有关行终止符的详细信息,请参阅备注部分的WriteLine不带参数的方法。For more information about the line terminator, see the Remarks section of the WriteLine method that takes no parameters.

安全性

UIPermission
用于修改安全顶级窗口和子窗口。for modifying safe top-level windows and subwindows. 关联的枚举: SafeTopLevelWindowsAssociated enumeration: SafeTopLevelWindows

另请参阅

WriteLine(String, Object[]) WriteLine(String, Object[]) WriteLine(String, Object[]) WriteLine(String, Object[])

使用指定的格式信息,将指定的对象数组(后跟当前行终止符)的文本表示形式写入标准输出流。Writes the text representation of the specified array of objects, followed by the current line terminator, to the standard output stream using the specified format information.

public:
 static void WriteLine(System::String ^ format, ... cli::array <System::Object ^> ^ arg);
public static void WriteLine (string format, params object[] arg);
static member WriteLine : string * obj[] -> unit
Public Shared Sub WriteLine (format As String, ParamArray arg As Object())

参数

format
String String String String

复合格式字符串。A composite format string.

arg
Object[]

要使用 format 写入的对象的数组。An array of objects to write using format.

异常

出现 I/O 错误。An I/O error occurred.

format 中的格式规范无效。The format specification in format is invalid.

示例

下面的示例演示数字、日期和枚举的标准格式说明符。The following example demonstrates the standard formatting specifiers for numbers, dates, and enumerations.

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

using namespace System;

public enum class Color {Yellow = 1, Blue, Green};

int main() 
{
    DateTime thisDate = DateTime::Now;
    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

*/
// 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

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

Imports System
Imports Microsoft.VisualBasic

Class Sample
   Public Enum Color
      Yellow = 1
      Blue = 2
      Green = 3
   End Enum 'Color
   Private Shared thisDate As DateTime = DateTime.Now
   
   Public Shared Sub 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}" & vbCrLf & _
                        "(D) Decimal:. . . . . . . . . {0:D}" & vbCrLf & _
                        "(E) Scientific: . . . . . . . {1:E}" & vbCrLf & _
                        "(F) Fixed point:. . . . . . . {1:F}" & vbCrLf & _
                        "(G) General:. . . . . . . . . {0:G}" & vbCrLf & _
                        "    (default):. . . . . . . . {0} (default = 'G')" & vbCrLf & _
                        "(N) Number: . . . . . . . . . {0:N}" & vbCrLf & _
                        "(P) Percent:. . . . . . . . . {1:P}" & vbCrLf & _
                        "(R) Round-trip: . . . . . . . {1:R}" & vbCrLf & _
                        "(X) Hexadecimal:. . . . . . . {0:X}" & vbCrLf, _
                        - 123, - 123.45F)

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

      ' Format a Color enumeration value in various ways.
      Console.WriteLine("Standard Enumeration Format Specifiers")
      Console.WriteLine("(G) General:. . . . . . . . . {0:G}" & vbCrLf & _
                        "    (default):. . . . . . . . {0} (default = 'G')" & vbCrLf & _
                        "(F) Flags:. . . . . . . . . . {0:F} (flags or integer)" & vbCrLf & _
                        "(D) Decimal number: . . . . . {0:D}" & vbCrLf & _
                        "(X) Hexadecimal:. . . . . . . {0:X}" & vbCrLf, _
                        Color.Green)
   End Sub 'Main
End Class 'Sample
'
'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方法来显示原始费用金额、 小费金额和总金额。The following example is a tip calculator that calculates an 18% tip and uses the WriteLine method to display the amount of the original charge, the amount of the tip, and the total amount. 该示例是要求用户提供的作为命令行参数的原始费用的控制台应用程序。The example is a console application that requires the user to supply the amount of the original charge as a command-line parameter.

using System;

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

/*
>tipcalc 52.23

Bill total:       $52.23
Tip total/rate:    $9.40 (18.0 %)
------------------------
Grand total:      $61.63
*/
Public Class TipCalculator
   Private Const tipRate As Double = 0.18
   
   Public Shared Sub Main()
      System.Environment.ExitCode = Calculator(System.Environment.GetCommandLineArgs())
   End Sub
   
   Public Shared Function Calculator(args() As String) As Integer
      Dim billTotal As Double
      If args.Length < 2 Then
         Console.WriteLine("usage: TIPCALC total")
         Return 1
      Else
         If Not Double.TryParse(args(1), billTotal) Then
            Console.WriteLine("usage: TIPCALC total")
            Return 1
         End If
         
         Dim tip As Double = billTotal * tipRate
         Console.WriteLine()
         Console.WriteLine("Bill total:{1}{0,8:c}", billTotal, vbTab)
         Console.WriteLine("Tip total/rate:{2}{0,8:c} ({1:p1})", tip, tipRate, vbTab)
         Console.WriteLine("".PadRight(24, "-"c))
         Console.WriteLine("Grand total:{1}{0,8:c}", billTotal + tip, vbTab)
         Return 0
      End If
   End Function 
End Class 

'Example Output:
'---------------
' >tipcalc 52.23
' 
' Bill total:       $52.23
' Tip total/rate:    $9.40 (18.0 %)
' ------------------------
' Grand total:      $61.63

注解

此方法使用复合格式设置功能的.NET Framework,才能将对象的值转换为其文本表示形式,在字符串中嵌入该表示形式。This method uses the composite formatting feature of the .NET Framework to convert the value of an object to its text representation and embed that representation in a string. 生成的字符串写入输出流。The resulting string is written to the output stream.

format参数由零个或多个文本序列与零个或多个索引占位符称为格式项,对应于此方法的参数列表中的对象。The format parameter consists of zero or more runs of text intermixed with zero or more indexed placeholders, called format items, that correspond to an object in the parameter list of this method. 格式设置过程将每个格式项替换相应的对象的值的文本表示形式。The formatting process replaces each format item with the text representation of the value of the corresponding object.

格式项的语法是{索引[,对齐] [:formatString]},它指定强制索引、 可选长度和格式的文本的对齐方式和可选的格式说明符字符用于控制如何设置相应的对象的值的格式字符串。The syntax of a format item is {index[,alignment][:formatString]}, which specifies a mandatory index, the optional length and alignment of the formatted text, and an optional string of format specifier characters that govern how the value of the corresponding object is formatted.

.NET Framework 提供了广泛的格式设置支持,其格式设置的以下主题中更详细地介绍。The .NET Framework provides extensive formatting support, which is described in greater detail in the following formatting topics.

有关行终止符的详细信息,请参阅备注部分的WriteLine不带参数的方法。For more information about the line terminator, see the Remarks section of the WriteLine method that takes no parameters.

调用方说明

此方法不由调用C++代码。This method is not called by C++ code. C++编译器解析对调用System.Console.WriteLine ,与调用包含一个字符串或一系列四个或多个对象参数WriteLine(String, Object, Object, Object, Object)The C++ compiler resolves calls to System.Console.WriteLine that include a string and a list of four or more object parameters as a call to WriteLine(String, Object, Object, Object, Object). 解析调用System.Console.WriteLine字符串和一个对象数组,包含与调用WriteLine(String, Object)It resolves calls to System.Console.WriteLine that include a string and an object array as a call to WriteLine(String, Object).

安全性

UIPermission
用于修改安全顶级窗口和子窗口。for modifying safe top-level windows and subwindows. 关联的枚举: SafeTopLevelWindowsAssociated enumeration: SafeTopLevelWindows

另请参阅

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

使用指定的格式信息,将指定对象(后跟当前行终止符)的文本表示形式写入标准输出流。Writes the text representation of the specified object, followed by the current line terminator, to the standard output stream using the specified format information.

public:
 static void WriteLine(System::String ^ format, System::Object ^ arg0);
public static void WriteLine (string format, object arg0);
static member WriteLine : string * obj -> unit
Public Shared Sub WriteLine (format As String, arg0 As Object)

参数

format
String String String String

复合格式字符串。A composite format string.

arg0
Object Object Object Object

要使用 format 写入的对象。An object to write using format.

异常

出现 I/O 错误。An I/O error occurred.

format 中的格式规范无效。The format specification in format is invalid.

示例

下面的示例调用WriteLine(String, Object)方法来显示五个随机生成的Boolean值。The following example calls the WriteLine(String, Object) method to display five randomly generated Boolean values.

using System;

public class Example
{
   public static void Main()
   {
      Random rnd = new Random();
      // Generate five random Boolean values.
      for (int ctr = 1; ctr <= 5; ctr++) {
         Boolean bln = Convert.ToBoolean(rnd.Next(0, 2));
         Console.WriteLine("True or False: {0}", bln);
      }
   }
}
// The example displays the following output:
//       True or False: False
//       True or False: True
//       True or False: False
//       True or False: False
//       True or False: True
Module Example
   Public Sub Main()
      Dim rnd As New Random()
      ' Generate five random Boolean values.
      For ctr As Integer = 1 To 5
         Dim bool As Boolean = Convert.ToBoolean(rnd.Next(0, 2))
         Console.WriteLine("True or False: {0}", bool)
      Next
   End Sub
End Module
' The example displays the following output:
'       True or False: False
'       True or False: True
'       True or False: False
'       True or False: False
'       True or False: True

下面的示例调用WriteLine(String, Object)方法来显示当前日期。The following example calls the WriteLine(String, Object) method to display the current date. 请注意,格式项中format参数使用"D"标准日期和时间格式字符串中的当前区域性的长日期格式显示日期。Note that the format item in the format argument uses the "D" standard date and time format string to display the date in the long date format of the current culture.

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: Friday, April 1, 2016
Module Example
   Public Sub Main()
      Console.WriteLine("Today's date: {0:D}", DateTime.Now)
   End Sub
End Module
' The example displays output like the following:
'       Today's date: Friday, April 1, 2016

注解

此方法使用复合格式设置功能的.NET Framework,才能将对象的值转换为其文本表示形式,在字符串中嵌入该表示形式。This method uses the composite formatting feature of the .NET Framework to convert the value of an object to its text representation and embed that representation in a string. 生成的字符串写入输出流。The resulting string is written to the output stream.

format参数由零个或多个文本序列与零个或多个索引占位符称为格式项,对应于此方法的参数列表中的对象。The format parameter consists of zero or more runs of text intermixed with zero or more indexed placeholders, called format items, that correspond to an object in the parameter list of this method. 格式设置过程将每个格式项替换相应的对象的值的文本表示形式。The formatting process replaces each format item with the text representation of the value of the corresponding object.

格式项的语法是{索引[,对齐] [:formatString]},它指定强制索引、 可选长度和格式的文本的对齐方式和可选的格式说明符字符用于控制如何设置相应的对象的值的格式字符串。The syntax of a format item is {index[,alignment][:formatString]}, which specifies a mandatory index, the optional length and alignment of the formatted text, and an optional string of format specifier characters that govern how the value of the corresponding object is formatted.

.NET Framework 提供了广泛的格式设置支持,其格式设置的以下主题中更详细地介绍。The .NET Framework provides extensive formatting support, which is described in greater detail in the following formatting topics.

有关行终止符的详细信息,请参阅备注部分的WriteLine不带参数的方法。For more information about the line terminator, see the Remarks section of the WriteLine method that takes no parameters.

安全性

UIPermission
用于修改安全顶级窗口和子窗口。for modifying safe top-level windows and subwindows. 关联的枚举: SafeTopLevelWindowsAssociated enumeration: SafeTopLevelWindows

另请参阅

WriteLine(UInt64) WriteLine(UInt64) WriteLine(UInt64) WriteLine(UInt64)

重要

此 API 不符合 CLS。

将指定的 64 位无符号的整数值的文本表示(后跟当前行的结束符)写入标准输出流。Writes the text representation of the specified 64-bit unsigned integer value, followed by the current line terminator, to the standard output stream.

public:
 static void WriteLine(System::UInt64 value);
[System.CLSCompliant(false)]
public static void WriteLine (ulong value);
static member WriteLine : uint64 -> unit
Public Shared Sub WriteLine (value As ULong)

参数

value
UInt64 UInt64 UInt64 UInt64

要写入的值。The value to write.

异常

出现 I/O 错误。An I/O error occurred.

示例

下面的示例是一个提示计算器计算 18%提示并使用WriteLine方法来显示原始费用金额、 小费金额和总金额。The following example is a tip calculator that calculates an 18% tip and uses the WriteLine method to display the amount of the original charge, the amount of the tip, and the total amount. 该示例是要求用户提供的作为命令行参数的原始费用的控制台应用程序。The example is a console application that requires the user to supply the amount of the original charge as a command-line parameter.

using System;

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

/*
>tipcalc 52.23

Bill total:       $52.23
Tip total/rate:    $9.40 (18.0 %)
------------------------
Grand total:      $61.63
*/
Public Class TipCalculator
   Private Const tipRate As Double = 0.18
   
   Public Shared Sub Main()
      System.Environment.ExitCode = Calculator(System.Environment.GetCommandLineArgs())
   End Sub
   
   Public Shared Function Calculator(args() As String) As Integer
      Dim billTotal As Double
      If args.Length < 2 Then
         Console.WriteLine("usage: TIPCALC total")
         Return 1
      Else
         If Not Double.TryParse(args(1), billTotal) Then
            Console.WriteLine("usage: TIPCALC total")
            Return 1
         End If
         
         Dim tip As Double = billTotal * tipRate
         Console.WriteLine()
         Console.WriteLine("Bill total:{1}{0,8:c}", billTotal, vbTab)
         Console.WriteLine("Tip total/rate:{2}{0,8:c} ({1:p1})", tip, tipRate, vbTab)
         Console.WriteLine("".PadRight(24, "-"c))
         Console.WriteLine("Grand total:{1}{0,8:c}", billTotal + tip, vbTab)
         Return 0
      End If
   End Function 
End Class 

'Example Output:
'---------------
' >tipcalc 52.23
' 
' Bill total:       $52.23
' Tip total/rate:    $9.40 (18.0 %)
' ------------------------
' Grand total:      $61.63

注解

文本表示形式value由调用UInt64.ToString方法。The text representation of value is produced by calling the UInt64.ToString method.

有关行终止符的详细信息,请参阅备注部分的WriteLine不带参数的方法。For more information about the line terminator, see the Remarks section of the WriteLine method that takes no parameters.

安全性

UIPermission
用于修改安全顶级窗口和子窗口。for modifying safe top-level windows and subwindows. 关联的枚举: SafeTopLevelWindowsAssociated enumeration: SafeTopLevelWindows

另请参阅

WriteLine(UInt32) WriteLine(UInt32) WriteLine(UInt32) WriteLine(UInt32)

重要

此 API 不符合 CLS。

将指定的 32 位无符号的整数值的文本表示(后跟当前行的结束符)写入标准输出流。Writes the text representation of the specified 32-bit unsigned integer value, followed by the current line terminator, to the standard output stream.

public:
 static void WriteLine(System::UInt32 value);
[System.CLSCompliant(false)]
public static void WriteLine (uint value);
static member WriteLine : uint32 -> unit
Public Shared Sub WriteLine (value As UInteger)

参数

value
UInt32 UInt32 UInt32 UInt32

要写入的值。The value to write.

异常

出现 I/O 错误。An I/O error occurred.

示例

下面的示例是一个提示计算器计算 18%提示并使用WriteLine方法来显示原始费用金额、 小费金额和总金额。The following example is a tip calculator that calculates an 18% tip and uses the WriteLine method to display the amount of the original charge, the amount of the tip, and the total amount. 该示例是要求用户提供的作为命令行参数的原始费用的控制台应用程序。The example is a console application that requires the user to supply the amount of the original charge as a command-line parameter.

using System;

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

/*
>tipcalc 52.23

Bill total:       $52.23
Tip total/rate:    $9.40 (18.0 %)
------------------------
Grand total:      $61.63
*/
Public Class TipCalculator
   Private Const tipRate As Double = 0.18
   
   Public Shared Sub Main()
      System.Environment.ExitCode = Calculator(System.Environment.GetCommandLineArgs())
   End Sub
   
   Public Shared Function Calculator(args() As String) As Integer
      Dim billTotal As Double
      If args.Length < 2 Then
         Console.WriteLine("usage: TIPCALC total")
         Return 1
      Else
         If Not Double.TryParse(args(1), billTotal) Then
            Console.WriteLine("usage: TIPCALC total")
            Return 1
         End If
         
         Dim tip As Double = billTotal * tipRate
         Console.WriteLine()
         Console.WriteLine("Bill total:{1}{0,8:c}", billTotal, vbTab)
         Console.WriteLine("Tip total/rate:{2}{0,8:c} ({1:p1})", tip, tipRate, vbTab)
         Console.WriteLine("".PadRight(24, "-"c))
         Console.WriteLine("Grand total:{1}{0,8:c}", billTotal + tip, vbTab)
         Return 0
      End If
   End Function 
End Class 

'Example Output:
'---------------
' >tipcalc 52.23
' 
' Bill total:       $52.23
' Tip total/rate:    $9.40 (18.0 %)
' ------------------------
' Grand total:      $61.63

注解

文本表示形式value由调用UInt32.ToString方法。The text representation of value is produced by calling the UInt32.ToString method.

有关行终止符的详细信息,请参阅备注部分的WriteLine不带参数的方法。For more information about the line terminator, see the Remarks section of the WriteLine method that takes no parameters.

安全性

UIPermission
用于修改安全顶级窗口和子窗口。for modifying safe top-level windows and subwindows. 关联的枚举: SafeTopLevelWindowsAssociated enumeration: SafeTopLevelWindows

另请参阅

WriteLine(Single) WriteLine(Single) WriteLine(Single) WriteLine(Single)

将指定的单精度浮点值的文本表示形式(后跟当前行终止符)写入标准输出流。Writes the text representation of the specified single-precision floating-point value, followed by the current line terminator, to the standard output stream.

public:
 static void WriteLine(float value);
public static void WriteLine (float value);
static member WriteLine : single -> unit
Public Shared Sub WriteLine (value As Single)

参数

value
Single Single Single Single

要写入的值。The value to write.

异常

出现 I/O 错误。An I/O error occurred.

示例

下面的示例是一个提示计算器计算 18%提示并使用WriteLine方法来显示原始费用金额、 小费金额和总金额。The following example is a tip calculator that calculates an 18% tip and uses the WriteLine method to display the amount of the original charge, the amount of the tip, and the total amount. 该示例是要求用户提供的作为命令行参数的原始费用的控制台应用程序。The example is a console application that requires the user to supply the amount of the original charge as a command-line parameter.

using System;

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

/*
>tipcalc 52.23

Bill total:       $52.23
Tip total/rate:    $9.40 (18.0 %)
------------------------
Grand total:      $61.63
*/
Public Class TipCalculator
   Private Const tipRate As Double = 0.18
   
   Public Shared Sub Main()
      System.Environment.ExitCode = Calculator(System.Environment.GetCommandLineArgs())
   End Sub
   
   Public Shared Function Calculator(args() As String) As Integer
      Dim billTotal As Double
      If args.Length < 2 Then
         Console.WriteLine("usage: TIPCALC total")
         Return 1
      Else
         If Not Double.TryParse(args(1), billTotal) Then
            Console.WriteLine("usage: TIPCALC total")
            Return 1
         End If
         
         Dim tip As Double = billTotal * tipRate
         Console.WriteLine()
         Console.WriteLine("Bill total:{1}{0,8:c}", billTotal, vbTab)
         Console.WriteLine("Tip total/rate:{2}{0,8:c} ({1:p1})", tip, tipRate, vbTab)
         Console.WriteLine("".PadRight(24, "-"c))
         Console.WriteLine("Grand total:{1}{0,8:c}", billTotal + tip, vbTab)
         Return 0
      End If
   End Function 
End Class 

'Example Output:
'---------------
' >tipcalc 52.23
' 
' Bill total:       $52.23
' Tip total/rate:    $9.40 (18.0 %)
' ------------------------
' Grand total:      $61.63

注解

文本表示形式value由调用Single.ToString方法。The text representation of value is produced by calling the Single.ToString method.

有关行终止符的详细信息,请参阅备注部分的WriteLine不带参数的方法。For more information about the line terminator, see the Remarks section of the WriteLine method that takes no parameters.

安全性

UIPermission
用于修改安全顶级窗口和子窗口。for modifying safe top-level windows and subwindows. 关联的枚举: SafeTopLevelWindowsAssociated enumeration: SafeTopLevelWindows

另请参阅

WriteLine(Decimal) WriteLine(Decimal) WriteLine(Decimal) WriteLine(Decimal)

将指定的 Decimal 值的文本表示形式(后跟当前行终止符)写入标准输出流。Writes the text representation of the specified Decimal value, followed by the current line terminator, to the standard output stream.

public:
 static void WriteLine(System::Decimal value);
public static void WriteLine (decimal value);
static member WriteLine : decimal -> unit
Public Shared Sub WriteLine (value As Decimal)

参数

value
Decimal Decimal Decimal Decimal

要写入的值。The value to write.

异常

出现 I/O 错误。An I/O error occurred.

示例

下面的示例是一个提示计算器计算 18%提示并使用WriteLine方法来显示原始费用金额、 小费金额和总金额。The following example is a tip calculator that calculates an 18% tip and uses the WriteLine method to display the amount of the original charge, the amount of the tip, and the total amount. 该示例是要求用户提供的作为命令行参数的原始费用的控制台应用程序。The example is a console application that requires the user to supply the amount of the original charge as a command-line parameter.

using System;

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

/*
>tipcalc 52.23

Bill total:       $52.23
Tip total/rate:    $9.40 (18.0 %)
------------------------
Grand total:      $61.63
*/
Public Class TipCalculator
   Private Const tipRate As Double = 0.18
   
   Public Shared Sub Main()
      System.Environment.ExitCode = Calculator(System.Environment.GetCommandLineArgs())
   End Sub
   
   Public Shared Function Calculator(args() As String) As Integer
      Dim billTotal As Double
      If args.Length < 2 Then
         Console.WriteLine("usage: TIPCALC total")
         Return 1
      Else
         If Not Double.TryParse(args(1), billTotal) Then
            Console.WriteLine("usage: TIPCALC total")
            Return 1
         End If
         
         Dim tip As Double = billTotal * tipRate
         Console.WriteLine()
         Console.WriteLine("Bill total:{1}{0,8:c}", billTotal, vbTab)
         Console.WriteLine("Tip total/rate:{2}{0,8:c} ({1:p1})", tip, tipRate, vbTab)
         Console.WriteLine("".PadRight(24, "-"c))
         Console.WriteLine("Grand total:{1}{0,8:c}", billTotal + tip, vbTab)
         Return 0
      End If
   End Function 
End Class 

'Example Output:
'---------------
' >tipcalc 52.23
' 
' Bill total:       $52.23
' Tip total/rate:    $9.40 (18.0 %)
' ------------------------
' Grand total:      $61.63

注解

文本表示形式value由调用Decimal.ToString方法。The text representation of value is produced by calling the Decimal.ToString method.

有关行终止符的详细信息,请参阅备注部分的WriteLine不带参数的方法。For more information about the line terminator, see the Remarks section of the WriteLine method that takes no parameters.

安全性

UIPermission
用于修改安全顶级窗口和子窗口。for modifying safe top-level windows and subwindows. 关联的枚举: SafeTopLevelWindowsAssociated enumeration: SafeTopLevelWindows

另请参阅

WriteLine(Int64) WriteLine(Int64) WriteLine(Int64) WriteLine(Int64)

将指定的 64 位有符号整数值的文本表示(后跟当前行的结束符)写入标准输出流。Writes the text representation of the specified 64-bit signed integer value, followed by the current line terminator, to the standard output stream.

public:
 static void WriteLine(long value);
public static void WriteLine (long value);
static member WriteLine : int64 -> unit
Public Shared Sub WriteLine (value As Long)

参数

value
Int64 Int64 Int64 Int64

要写入的值。The value to write.

异常

出现 I/O 错误。An I/O error occurred.

示例

下面的示例是一个提示计算器计算 18%提示并使用WriteLine方法来显示原始费用金额、 小费金额和总金额。The following example is a tip calculator that calculates an 18% tip and uses the WriteLine method to display the amount of the original charge, the amount of the tip, and the total amount. 该示例是要求用户提供的作为命令行参数的原始费用的控制台应用程序。The example is a console application that requires the user to supply the amount of the original charge as a command-line parameter.

using System;

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

/*
>tipcalc 52.23

Bill total:       $52.23
Tip total/rate:    $9.40 (18.0 %)
------------------------
Grand total:      $61.63
*/
Public Class TipCalculator
   Private Const tipRate As Double = 0.18
   
   Public Shared Sub Main()
      System.Environment.ExitCode = Calculator(System.Environment.GetCommandLineArgs())
   End Sub
   
   Public Shared Function Calculator(args() As String) As Integer
      Dim billTotal As Double
      If args.Length < 2 Then
         Console.WriteLine("usage: TIPCALC total")
         Return 1
      Else
         If Not Double.TryParse(args(1), billTotal) Then
            Console.WriteLine("usage: TIPCALC total")
            Return 1
         End If
         
         Dim tip As Double = billTotal * tipRate
         Console.WriteLine()
         Console.WriteLine("Bill total:{1}{0,8:c}", billTotal, vbTab)
         Console.WriteLine("Tip total/rate:{2}{0,8:c} ({1:p1})", tip, tipRate, vbTab)
         Console.WriteLine("".PadRight(24, "-"c))
         Console.WriteLine("Grand total:{1}{0,8:c}", billTotal + tip, vbTab)
         Return 0
      End If
   End Function 
End Class 

'Example Output:
'---------------
' >tipcalc 52.23
' 
' Bill total:       $52.23
' Tip total/rate:    $9.40 (18.0 %)
' ------------------------
' Grand total:      $61.63

注解

文本表示形式value由调用Int64.ToString方法。The text representation of value is produced by calling the Int64.ToString method.

有关行终止符的详细信息,请参阅备注部分的WriteLine不带参数的方法。For more information about the line terminator, see the Remarks section of the WriteLine method that takes no parameters.

安全性

UIPermission
用于修改安全顶级窗口和子窗口。for modifying safe top-level windows and subwindows. 关联的枚举: SafeTopLevelWindowsAssociated enumeration: SafeTopLevelWindows

另请参阅

WriteLine(Int32) WriteLine(Int32) WriteLine(Int32) WriteLine(Int32)

将指定的 32 位有符号整数值的文本表示(后跟当前行的结束符)写入标准输出流。Writes the text representation of the specified 32-bit signed integer value, followed by the current line terminator, to the standard output stream.

public:
 static void WriteLine(int value);
public static void WriteLine (int value);
static member WriteLine : int -> unit
Public Shared Sub WriteLine (value As Integer)

参数

value
Int32 Int32 Int32 Int32

要写入的值。The value to write.

异常

出现 I/O 错误。An I/O error occurred.

示例

下面的示例是一个提示计算器计算 18%提示并使用WriteLine方法来显示原始费用金额、 小费金额和总金额。The following example is a tip calculator that calculates an 18% tip and uses the WriteLine method to display the amount of the original charge, the amount of the tip, and the total amount. 该示例是要求用户提供的作为命令行参数的原始费用的控制台应用程序。The example is a console application that requires the user to supply the amount of the original charge as a command-line parameter.

using System;

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

/*
>tipcalc 52.23

Bill total:       $52.23
Tip total/rate:    $9.40 (18.0 %)
------------------------
Grand total:      $61.63
*/
Public Class TipCalculator
   Private Const tipRate As Double = 0.18
   
   Public Shared Sub Main()
      System.Environment.ExitCode = Calculator(System.Environment.GetCommandLineArgs())
   End Sub
   
   Public Shared Function Calculator(args() As String) As Integer
      Dim billTotal As Double
      If args.Length < 2 Then
         Console.WriteLine("usage: TIPCALC total")
         Return 1
      Else
         If Not Double.TryParse(args(1), billTotal) Then
            Console.WriteLine("usage: TIPCALC total")
            Return 1
         End If
         
         Dim tip As Double = billTotal * tipRate
         Console.WriteLine()
         Console.WriteLine("Bill total:{1}{0,8:c}", billTotal, vbTab)
         Console.WriteLine("Tip total/rate:{2}{0,8:c} ({1:p1})", tip, tipRate, vbTab)
         Console.WriteLine("".PadRight(24, "-"c))
         Console.WriteLine("Grand total:{1}{0,8:c}", billTotal + tip, vbTab)
         Return 0
      End If
   End Function 
End Class 

'Example Output:
'---------------
' >tipcalc 52.23
' 
' Bill total:       $52.23
' Tip total/rate:    $9.40 (18.0 %)
' ------------------------
' Grand total:      $61.63

注解

文本表示形式value由调用Int32.ToString方法。The text representation of value is produced by calling the Int32.ToString method.

有关行终止符的详细信息,请参阅备注部分的WriteLine不带参数的方法。For more information about the line terminator, see the Remarks section of the WriteLine method that takes no parameters.

安全性

UIPermission
用于修改安全顶级窗口和子窗口。for modifying safe top-level windows and subwindows. 关联的枚举: SafeTopLevelWindowsAssociated enumeration: SafeTopLevelWindows

另请参阅

WriteLine(Double) WriteLine(Double) WriteLine(Double) WriteLine(Double)

将指定的双精度浮点值的文本表示形式(后跟当前行终止符)写入标准输出流。Writes the text representation of the specified double-precision floating-point value, followed by the current line terminator, to the standard output stream.

public:
 static void WriteLine(double value);
public static void WriteLine (double value);
static member WriteLine : double -> unit
Public Shared Sub WriteLine (value As Double)

参数

value
Double Double Double Double

要写入的值。The value to write.

异常

出现 I/O 错误。An I/O error occurred.

示例

下面的示例是一个提示计算器计算 18%提示并使用WriteLine方法来显示原始费用金额、 小费金额和总金额。The following example is a tip calculator that calculates an 18% tip and uses the WriteLine method to display the amount of the original charge, the amount of the tip, and the total amount. 该示例是要求用户提供的作为命令行参数的原始费用的控制台应用程序。The example is a console application that requires the user to supply the amount of the original charge as a command-line parameter.

using System;

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

/*
>tipcalc 52.23

Bill total:       $52.23
Tip total/rate:    $9.40 (18.0 %)
------------------------
Grand total:      $61.63
*/
Public Class TipCalculator
   Private Const tipRate As Double = 0.18
   
   Public Shared Sub Main()
      System.Environment.ExitCode = Calculator(System.Environment.GetCommandLineArgs())
   End Sub
   
   Public Shared Function Calculator(args() As String) As Integer
      Dim billTotal As Double
      If args.Length < 2 Then
         Console.WriteLine("usage: TIPCALC total")
         Return 1
      Else
         If Not Double.TryParse(args(1), billTotal) Then
            Console.WriteLine("usage: TIPCALC total")
            Return 1
         End If
         
         Dim tip As Double = billTotal * tipRate
         Console.WriteLine()
         Console.WriteLine("Bill total:{1}{0,8:c}", billTotal, vbTab)
         Console.WriteLine("Tip total/rate:{2}{0,8:c} ({1:p1})", tip, tipRate, vbTab)
         Console.WriteLine("".PadRight(24, "-"c))
         Console.WriteLine("Grand total:{1}{0,8:c}", billTotal + tip, vbTab)
         Return 0
      End If
   End Function 
End Class 

'Example Output:
'---------------
' >tipcalc 52.23
' 
' Bill total:       $52.23
' Tip total/rate:    $9.40 (18.0 %)
' ------------------------
' Grand total:      $61.63

注解

文本表示形式value由调用Double.ToString方法。The text representation of value is produced by calling the Double.ToString method.

有关行终止符的详细信息,请参阅备注部分的WriteLine不带参数的方法。For more information about the line terminator, see the Remarks section of the WriteLine method that takes no parameters.

安全性

UIPermission
用于修改安全顶级窗口和子窗口。for modifying safe top-level windows and subwindows. 关联的枚举: SafeTopLevelWindowsAssociated enumeration: SafeTopLevelWindows

另请参阅

WriteLine(Char[]) WriteLine(Char[]) WriteLine(Char[]) WriteLine(Char[])

将指定的 Unicode 字符数组(后跟当前行终止符)写入标准输出流。Writes the specified array of Unicode characters, followed by the current line terminator, to the standard output stream.

public:
 static void WriteLine(cli::array <char> ^ buffer);
public static void WriteLine (char[] buffer);
static member WriteLine : char[] -> unit
Public Shared Sub WriteLine (buffer As Char())

参数

buffer
Char[]

Unicode 字符数组。A Unicode character array.

异常

出现 I/O 错误。An I/O error occurred.

注解

有关行终止符的详细信息,请参阅备注部分的WriteLine不带参数的方法。For more information about the line terminator, see the Remarks section of the WriteLine method that takes no parameters.

安全性

UIPermission
用于修改安全顶级窗口和子窗口。for modifying safe top-level windows and subwindows. 关联的枚举: SafeTopLevelWindowsAssociated enumeration: SafeTopLevelWindows

另请参阅

WriteLine(Char) WriteLine(Char) WriteLine(Char) WriteLine(Char)

将指定的 Unicode 字符值(后跟当前行终止符)写入标准输出流。Writes the specified Unicode character, followed by the current line terminator, value to the standard output stream.

public:
 static void WriteLine(char value);
public static void WriteLine (char value);
static member WriteLine : char -> unit
Public Shared Sub WriteLine (value As Char)

参数

value
Char Char Char Char

要写入的值。The value to write.

异常

出现 I/O 错误。An I/O error occurred.

示例

下面的示例是一个提示计算器计算 18%提示并使用WriteLine方法来显示原始费用金额、 小费金额和总金额。The following example is a tip calculator that calculates an 18% tip and uses the WriteLine method to display the amount of the original charge, the amount of the tip, and the total amount. 该示例是要求用户提供的作为命令行参数的原始费用的控制台应用程序。The example is a console application that requires the user to supply the amount of the original charge as a command-line parameter.

using System;

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

/*
>tipcalc 52.23

Bill total:       $52.23
Tip total/rate:    $9.40 (18.0 %)
------------------------
Grand total:      $61.63
*/
Public Class TipCalculator
   Private Const tipRate As Double = 0.18
   
   Public Shared Sub Main()
      System.Environment.ExitCode = Calculator(System.Environment.GetCommandLineArgs())
   End Sub
   
   Public Shared Function Calculator(args() As String) As Integer
      Dim billTotal As Double
      If args.Length < 2 Then
         Console.WriteLine("usage: TIPCALC total")
         Return 1
      Else
         If Not Double.TryParse(args(1), billTotal) Then
            Console.WriteLine("usage: TIPCALC total")
            Return 1
         End If
         
         Dim tip As Double = billTotal * tipRate
         Console.WriteLine()
         Console.WriteLine("Bill total:{1}{0,8:c}", billTotal, vbTab)
         Console.WriteLine("Tip total/rate:{2}{0,8:c} ({1:p1})", tip, tipRate, vbTab)
         Console.WriteLine("".PadRight(24, "-"c))
         Console.WriteLine("Grand total:{1}{0,8:c}", billTotal + tip, vbTab)
         Return 0
      End If
   End Function 
End Class 

'Example Output:
'---------------
' >tipcalc 52.23
' 
' Bill total:       $52.23
' Tip total/rate:    $9.40 (18.0 %)
' ------------------------
' Grand total:      $61.63

注解

有关行终止符的详细信息,请参阅备注部分的WriteLine不带参数的方法。For more information about the line terminator, see the Remarks section of the WriteLine method that takes no parameters.

安全性

UIPermission
用于修改安全顶级窗口和子窗口。for modifying safe top-level windows and subwindows. 关联的枚举: SafeTopLevelWindowsAssociated enumeration: SafeTopLevelWindows

另请参阅

WriteLine(Boolean) WriteLine(Boolean) WriteLine(Boolean) WriteLine(Boolean)

将指定布尔值的文本表示形式(后跟当前行终止符)写入标准输出流。Writes the text representation of the specified Boolean value, followed by the current line terminator, to the standard output stream.

public:
 static void WriteLine(bool value);
public static void WriteLine (bool value);
static member WriteLine : bool -> unit
Public Shared Sub WriteLine (value As Boolean)

参数

value
Boolean Boolean Boolean Boolean

要写入的值。The value to write.

异常

出现 I/O 错误。An I/O error occurred.

示例

下面的示例生成 10 个随机整数,并使用Console.WriteLine(Boolean)方法,以指示它们是否即使。The following example generates ten random integers and uses the Console.WriteLine(Boolean) method to indicate whether they are even.

using namespace System;

void main()
{
   // Assign 10 random integers to an array.
   Random^ rnd = gcnew Random();
   array<Int32>^ numbers = gcnew array<Int32>(10); 
   for (int ctr = 0; ctr <= numbers->GetUpperBound(0); ctr++)
      numbers[ctr] = rnd->Next();

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

public class Example
{
   public static void Main()
   {
      // 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();      
      }
   }
}
Module Example
   Public Sub Main()
      ' Assign 10 random integers to an array.
      Dim rnd As New Random()
      Dim numbers(9) As Integer
      For ctr As Integer = 0 To numbers.GetUpperBound(0)
         numbers(ctr) = rnd.Next
      Next
      
      ' Determine whether the numbers are even or odd.
      For Each number In numbers
         Dim even As Boolean = (number mod 2 = 0)
         Console.WriteLine("Is {0} even:", number)
         Console.WriteLine(even)
         Console.WriteLine()      
      Next
   End Sub
End Module

注解

文本表示形式value由调用Boolean.ToString方法。The text representation of value is produced by calling the Boolean.ToString method.

有关行终止符的详细信息,请参阅备注部分的WriteLine不带参数的方法。For more information about the line terminator, see the Remarks section of the WriteLine method that takes no parameters.

安全性

UIPermission
用于修改安全顶级窗口和子窗口。for modifying safe top-level windows and subwindows. 关联的枚举: SafeTopLevelWindowsAssociated enumeration: SafeTopLevelWindows

另请参阅

WriteLine() WriteLine() WriteLine() WriteLine()

将当前行终止符写入标准输出流。Writes the current line terminator to the standard output stream.

public:
 static void WriteLine();
public static void WriteLine ();
static member WriteLine : unit -> unit
Public Shared Sub WriteLine ()

异常

出现 I/O 错误。An I/O error occurred.

示例

该示例将更改其默认值为"\r\n"行终止符或vbCrLf到"\r\n\r\n"或vbCrLf + vbCrLfThe example changes the line terminator from its default value of "\r\n" or vbCrLf to "\r\n\r\n" or vbCrLf + vbCrLf. 然后,它调用WriteLine()WriteLine(String)输出显示到控制台的方法。It then calls the WriteLine() and WriteLine(String) methods to display output to the console.

using namespace System;

void main()
{
   array<String^>^ lines = gcnew array<String^> { "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();
   for each (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();
   for each (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.
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.
Module Example
   Public Sub Main()
      Dim lines() As String = { "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()
      For Each line As String In lines
         Console.WriteLine(line)
      Next
      Console.WriteLine()
      
      ' Redefine the newline characters to double space.
      Console.Out.NewLine = vbCrLf + vbCrLf
      ' Output the lines using the new newline sequence.
      Console.WriteLine("With redefined new line characters:")
      Console.WriteLine()
      For Each line As String In lines
         Console.WriteLine(line)
      Next
   End Sub
End Module
' 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.

注解

默认行终止符是其值是回车符后跟换行符的字符串 ("\r\n"在 C# 中,或vbCrLf在 Visual Basic 中)。The default line terminator is a string whose value is a carriage return followed by a line feed ("\r\n" in C#, or vbCrLf in Visual Basic). 您可以通过设置更改的行终止符TextWriter.NewLine属性的Out到另一个字符串属性。You can change the line terminator by setting the TextWriter.NewLine property of the Out property to another string. 说明如示例所示。The example provides an illustration.

安全性

UIPermission
用于修改安全顶级窗口和子窗口。for modifying safe top-level windows and subwindows. 关联的枚举: SafeTopLevelWindowsAssociated enumeration: SafeTopLevelWindows

另请参阅

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

使用指定的格式信息,将指定对象的文本表示形式(后跟当前行终止符)写入标准输出流。Writes the text representation of the specified objects, followed by the current line terminator, to the standard output stream using the specified format information.

public:
 static void WriteLine(System::String ^ format, System::Object ^ arg0, System::Object ^ arg1, System::Object ^ arg2);
public static void WriteLine (string format, object arg0, object arg1, object arg2);
static member WriteLine : string * obj * obj * obj -> unit
Public Shared Sub WriteLine (format As String, arg0 As Object, arg1 As Object, arg2 As Object)

参数

format
String String String String

复合格式字符串。A composite format string.

arg0
Object Object Object Object

要使用 format 写入的第一个对象。The first object to write using format.

arg1
Object Object Object Object

要使用 format 写入的第二个对象。The second object to write using format.

arg2
Object Object Object Object

要使用 format 写入的第三个对象。The third object to write using format.

异常

出现 I/O 错误。An I/O error occurred.

format 中的格式规范无效。The format specification in format is invalid.

示例

下面的示例演示数字、日期和枚举的标准格式说明符。The following example demonstrates the standard formatting specifiers for numbers, dates, and enumerations.

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

using namespace System;

public enum class Color {Yellow = 1, Blue, Green};

int main() 
{
    DateTime thisDate = DateTime::Now;
    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

*/
// 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

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

Imports System
Imports Microsoft.VisualBasic

Class Sample
   Public Enum Color
      Yellow = 1
      Blue = 2
      Green = 3
   End Enum 'Color
   Private Shared thisDate As DateTime = DateTime.Now
   
   Public Shared Sub 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}" & vbCrLf & _
                        "(D) Decimal:. . . . . . . . . {0:D}" & vbCrLf & _
                        "(E) Scientific: . . . . . . . {1:E}" & vbCrLf & _
                        "(F) Fixed point:. . . . . . . {1:F}" & vbCrLf & _
                        "(G) General:. . . . . . . . . {0:G}" & vbCrLf & _
                        "    (default):. . . . . . . . {0} (default = 'G')" & vbCrLf & _
                        "(N) Number: . . . . . . . . . {0:N}" & vbCrLf & _
                        "(P) Percent:. . . . . . . . . {1:P}" & vbCrLf & _
                        "(R) Round-trip: . . . . . . . {1:R}" & vbCrLf & _
                        "(X) Hexadecimal:. . . . . . . {0:X}" & vbCrLf, _
                        - 123, - 123.45F)

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

      ' Format a Color enumeration value in various ways.
      Console.WriteLine("Standard Enumeration Format Specifiers")
      Console.WriteLine("(G) General:. . . . . . . . . {0:G}" & vbCrLf & _
                        "    (default):. . . . . . . . {0} (default = 'G')" & vbCrLf & _
                        "(F) Flags:. . . . . . . . . . {0:F} (flags or integer)" & vbCrLf & _
                        "(D) Decimal number: . . . . . {0:D}" & vbCrLf & _
                        "(X) Hexadecimal:. . . . . . . {0:X}" & vbCrLf, _
                        Color.Green)
   End Sub 'Main
End Class 'Sample
'
'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方法来显示原始费用金额、 小费金额和总金额。The following example is a tip calculator that calculates an 18% tip and uses the WriteLine method to display the amount of the original charge, the amount of the tip, and the total amount. 该示例是要求用户提供的作为命令行参数的原始费用的控制台应用程序。The example is a console application that requires the user to supply the amount of the original charge as a command-line parameter.

using System;

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

/*
>tipcalc 52.23

Bill total:       $52.23
Tip total/rate:    $9.40 (18.0 %)
------------------------
Grand total:      $61.63
*/
Public Class TipCalculator
   Private Const tipRate As Double = 0.18
   
   Public Shared Sub Main()
      System.Environment.ExitCode = Calculator(System.Environment.GetCommandLineArgs())
   End Sub
   
   Public Shared Function Calculator(args() As String) As Integer
      Dim billTotal As Double
      If args.Length < 2 Then
         Console.WriteLine("usage: TIPCALC total")
         Return 1
      Else
         If Not Double.TryParse(args(1), billTotal) Then
            Console.WriteLine("usage: TIPCALC total")
            Return 1
         End If
         
         Dim tip As Double = billTotal * tipRate
         Console.WriteLine()
         Console.WriteLine("Bill total:{1}{0,8:c}", billTotal, vbTab)
         Console.WriteLine("Tip total/rate:{2}{0,8:c} ({1:p1})", tip, tipRate, vbTab)
         Console.WriteLine("".PadRight(24, "-"c))
         Console.WriteLine("Grand total:{1}{0,8:c}", billTotal + tip, vbTab)
         Return 0
      End If
   End Function 
End Class 

'Example Output:
'---------------
' >tipcalc 52.23
' 
' Bill total:       $52.23
' Tip total/rate:    $9.40 (18.0 %)
' ------------------------
' Grand total:      $61.63

注解

此方法使用复合格式设置功能的.NET Framework,才能将对象的值转换为其文本表示形式,在字符串中嵌入该表示形式。This method uses the composite formatting feature of the .NET Framework to convert the value of an object to its text representation and embed that representation in a string. 生成的字符串写入输出流。The resulting string is written to the output stream.

format参数由零个或多个文本序列与零个或多个索引占位符称为格式项,对应于此方法的参数列表中的对象。The format parameter consists of zero or more runs of text intermixed with zero or more indexed placeholders, called format items, that correspond to an object in the parameter list of this method. 格式设置过程将每个格式项替换相应的对象的值的文本表示形式。The formatting process replaces each format item with the text representation of the value of the corresponding object.

格式项的语法是{索引[,对齐] [:formatString]},它指定强制索引、 可选长度和格式的文本的对齐方式和可选的格式说明符字符用于控制如何设置相应的对象的值的格式字符串。The syntax of a format item is {index[,alignment][:formatString]}, which specifies a mandatory index, the optional length and alignment of the formatted text, and an optional string of format specifier characters that govern how the value of the corresponding object is formatted.

.NET Framework 提供了广泛的格式设置支持,其格式设置的以下主题中更详细地介绍。The .NET Framework provides extensive formatting support, which is described in greater detail in the following formatting topics.

有关行终止符的详细信息,请参阅备注部分的WriteLine不带参数的方法。For more information about the line terminator, see the Remarks section of the WriteLine method that takes no parameters.

安全性

UIPermission
用于修改安全顶级窗口和子窗口。for modifying safe top-level windows and subwindows. 关联的枚举: SafeTopLevelWindowsAssociated enumeration: SafeTopLevelWindows

另请参阅

WriteLine(Object) WriteLine(Object) WriteLine(Object) WriteLine(Object)

将指定对象的文本表示形式(后跟当前行终止符)写入标准输出流。Writes the text representation of the specified object, followed by the current line terminator, to the standard output stream.

public:
 static void WriteLine(System::Object ^ value);
public static void WriteLine (object value);
static member WriteLine : obj -> unit
Public Shared Sub WriteLine (value As Object)

参数

value
Object Object Object Object

要写入的值。The value to write.

异常

出现 I/O 错误。An I/O error occurred.

示例

下面的示例使用WriteLine(Object)方法来显示到控制台对象数组中的每个值。The following example uses the WriteLine(Object) method to display each value in an object array to the console.

using namespace System;

void main()
{
   array<Object^>^ values = { true, 12.632, 17908, "stringValue",
                              'a', (Decimal) 16907.32 };
   for each (Object^ value in values)
      Console::WriteLine(value);
}
// The example displays the following output:
//    True
//    12.632
//    17908
//    stringValue
//    a
//    16907.32
using System;

public class Example
{
   public static void Main()
   {
      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
Module Example
   Public Sub Main()
      Dim values() As Object = { True, 12.632, 17908, "stringValue",
                                 "a"c, 16907.32d }
      For Each value In values
         Console.WriteLine(value)
      Next                           
   End Sub
End Module
' The example displays the following output:
'    True
'    12.632
'    17908
'    stringValue
'    a
'    16907.32

注解

如果 valuenull,则只写入行终止符。If value is null, only the line terminator is written. 否则为ToString方法的value调用以生成其字符串表示形式,并且生成的字符串写入到标准输出流。Otherwise, the ToString method of value is called to produce its string representation, and the resulting string is written to the standard output stream.

有关行终止符的详细信息,请参阅备注部分的WriteLine不带参数的方法。For more information about the line terminator, see the Remarks section of the WriteLine method that takes no parameters.

安全性

UIPermission
用于修改安全顶级窗口和子窗口。for modifying safe top-level windows and subwindows. 关联的枚举: SafeTopLevelWindowsAssociated enumeration: SafeTopLevelWindows

另请参阅

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

重要

此 API 不符合 CLS。

使用指定的格式信息,将指定的对象和可变长度参数列表(后跟当前行终止符)的文本表示形式写入标准输出流。Writes the text representation of the specified objects and variable-length parameter list, followed by the current line terminator, to the standard output stream using the specified format information.

public:
 static void WriteLine(System::String ^ format, System::Object ^ arg0, System::Object ^ arg1, System::Object ^ arg2, System::Object ^ arg3);
[System.CLSCompliant(false)]
public static void WriteLine (string format, object arg0, object arg1, object arg2, object arg3);
static member WriteLine : string * obj * obj * obj * obj -> unit
Public Shared Sub WriteLine (format As String, arg0 As Object, arg1 As Object, arg2 As Object, arg3 As Object)

参数

format
String String String String

复合格式字符串。A composite format string.

arg0
Object Object Object Object

要使用 format 写入的第一个对象。The first object to write using format.

arg1
Object Object Object Object

要使用 format 写入的第二个对象。The second object to write using format.

arg2
Object Object Object Object

要使用 format 写入的第三个对象。The third object to write using format.

arg3
Object Object Object Object

要使用 format 写入的第四个对象。The fourth object to write using format.

异常

出现 I/O 错误。An I/O error occurred.

format 中的格式规范无效。The format specification in format is invalid.

示例

下面的示例演示如何使用变量参数使用WriteLine(String, Object, Object, Object, Object)方法。The following example illustrates the use of variable arguments with the WriteLine(String, Object, Object, Object, Object) method. 使用复合格式字符串和五个格式项调用该方法。The method is called with a composite format string and five format items.

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。This API is not CLS-compliant. 符合 CLS 的替代方法是 Console.WriteLine(String, Object[])The CLS-compliant alternative is Console.WriteLine(String, Object[]). C# 和 Visual Basic 编译器会自动解决调用此方法与调用Console.WriteLine(String, Object[])The C# and Visual Basic compilers automatically resolve a call to this method as a call to Console.WriteLine(String, Object[]).

此方法使用复合格式设置功能的.NET Framework,才能将对象的值转换为其文本表示形式,在字符串中嵌入该表示形式。This method uses the composite formatting feature of the .NET Framework to convert the value of an object to its text representation and embed that representation in a string. 生成的字符串写入输出流。The resulting string is written to the output stream.

format参数由零个或多个文本序列与零个或多个索引占位符称为格式项,对应于此方法的参数列表中的对象。The format parameter consists of zero or more runs of text intermixed with zero or more indexed placeholders, called format items, that correspond to an object in the parameter list of this method. 格式设置过程将每个格式项替换相应的对象的值的文本表示形式。The formatting process replaces each format item with the text representation of the value of the corresponding object.

格式项的语法是{索引[,对齐] [:formatString]},它指定强制索引、 可选长度和格式的文本的对齐方式和可选的格式说明符字符用于控制如何设置相应的对象的值的格式字符串。The syntax of a format item is {index[,alignment][:formatString]}, which specifies a mandatory index, the optional length and alignment of the formatted text, and an optional string of format specifier characters that govern how the value of the corresponding object is formatted.

.NET Framework 提供了广泛的格式设置支持,其格式设置的以下主题中更详细地介绍。The .NET Framework provides extensive formatting support, which is described in greater detail in the following formatting topics.

有关行终止符的详细信息,请参阅备注部分的WriteLine不带参数的方法。For more information about the line terminator, see the Remarks section of the WriteLine method that takes no parameters.

调用方说明

此方法将标有vararg关键字,这意味着它支持个数可变的参数。This method is marked with the vararg keyword, which means that it supports a variable number of parameters. 可以从视觉对象调用方法C++,但它不能从调用C#或 Visual Basic 代码。The method can be called from Visual C++, but it cannot be called from C# or Visual Basic code. C# 和 Visual Basic 编译器解决对调用WriteLine(String, Object, Object, Object, Object)一样调用WriteLine(String, Object[])The C# and Visual Basic compilers resolve calls to WriteLine(String, Object, Object, Object, Object) as calls to WriteLine(String, Object[]).

安全性

UIPermission
用于修改安全顶级窗口和子窗口。for modifying safe top-level windows and subwindows. 关联的枚举: SafeTopLevelWindowsAssociated enumeration: SafeTopLevelWindows

另请参阅

适用于