Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Write Method (String, Object, Object, Object, Object)

Console.Write Method (String, Object, Object, Object, Object)

Writes the text representation of the specified objects and variable-length parameter list to the standard output stream using the specified format information.

This API is not CLS-compliant. The CLS-compliant alternative is Write(String, Object[]).

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

[CLSCompliantAttribute(false)]
[HostProtectionAttribute(SecurityAction.LinkDemand, UI = true)]
public static void Write(
	string format,
	Object arg0,
	Object arg1,
	Object arg2,
	Object arg3
)

Parameters

format
Type: System.String

A composite format string (see Remarks).

arg0
Type: System.Object

The first object to write using format.

arg1
Type: System.Object

The second object to write using format.

arg2
Type: System.Object

The third object to write using format.

arg3
Type: System.Object

The fourth object to write using format.

ExceptionCondition
IOException

An I/O error occurred.

ArgumentNullException

format is null.

FormatException

The format specification in format is invalid.

NoteNote:

The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: UI. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.

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.

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.

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.

The .NET Framework provides extensive formatting support, which is described in greater detail in the following formatting topics.

The following code example uses the WriteLine method to demonstrate 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 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

*/

The following code example illustrates the use of the Write method.

// This code example demonstrates the Console.WriteLine() and 
// Console.Write() methods that take 4 mandatory arguments  
// and an optional variable argument list. 

using System;

class Sample 
{
    public static void Main() 
    {
    int     arg0 = 1;
    long    arg1 = 222222;
    float   arg2 = -3.3F;
    double  arg3 = 5.5D;
    string  opt4 = "First optional argument.";
    char    opt5 = 'X';
    bool    opt6 = true;

// Display 4 mandatory and 3 optional arguments using the  
// Console.WriteLine method.
    Console.WriteLine("--- Console.WriteLine ---");
    Console.WriteLine("Mandatory arguments:\n" +
                      "argument 0: {0}\n" +
                      "argument 1: {1}\n" +
                      "argument 2: {2}\n" +
                      "argument 3: {3}\n\n" +
                      "Optional arguments:\n" +
                      "optional 4: {4}\n" +
                      "optional 5: {5}\n" +
                      "optional 6: {6}\n", 
                      arg0, arg1, arg2, arg3,
                      opt4, opt5, opt6);

// Display 4 mandatory and 3 optional arguments using the  
// Console.Write method. 
    Console.Write("--- Console.Write ---");
    Console.WriteLine();
    Console.Write("Mandatory arguments:\n" +
                      "argument 0: {0}\n" +
                      "argument 1: {1}\n" +
                      "argument 2: {2}\n" +
                      "argument 3: {3}\n\n" +
                      "Optional arguments:\n" +
                      "optional 4: {4}\n" +
                      "optional 5: {5}\n" +
                      "optional 6: {6}\n", 
                      arg0, arg1, arg2, arg3,
                      opt4, opt5, opt6);
    Console.WriteLine();
    }
}
/*
This code example produces the following results:

--- Console.WriteLine ---
Mandatory arguments:
argument 0: 1
argument 1: 222222
argument 2: -3.3
argument 3: 5.5

Optional arguments:
optional 4: First optional argument.
optional 5: X
optional 6: True

--- Console.Write ---
Mandatory arguments:
argument 0: 1
argument 1: 222222
argument 2: -3.3
argument 3: 5.5

Optional arguments:
optional 4: First optional argument.
optional 5: X
optional 6: True

*/

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2015 Microsoft