Export (0) Print
Expand All

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 method is not CLS-compliant.  The CLS-compliant alternative is Write(String,Object[]).

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

'Declaration
<CLSCompliantAttribute(False)> _
Public Shared Sub Write ( _
	format As String, _
	arg0 As Object, _
	arg1 As Object, _
	arg2 As Object, _
	arg3 As Object _
)
'Usage
Dim format As String
Dim arg0 As Object
Dim arg1 As Object
Dim arg2 As Object
Dim arg3 As Object

Console.Write(format, arg0, arg1, arg2, arg3)
/** @attribute CLSCompliantAttribute(false) */ 
public static void Write (
	String format, 
	Object arg0, 
	Object arg1, 
	Object arg2, 
	Object arg3
)
CLSCompliantAttribute(false) 
public static function Write (
	format : String, 
	arg0 : Object, 
	arg1 : Object, 
	arg2 : Object, 
	arg3 : Object
)

Parameters

format

The format string.

arg0

The first object to write using format.

arg1

The second object to write using format.

arg2

The third object to write using format.

arg3

The fourth object to write using format.

Exception typeCondition

IOException

An I/O error occurred.

ArgumentNullException

format is a null reference (Nothing in Visual Basic).

FormatException

The format specification in format is invalid.

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.

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 sortable: . . . {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 sortable: . . . 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.

Imports System

Class Sample
    Public Shared Sub Main() 
        Dim arg0 As Integer = 1
        Dim arg1 As Long = 222222
        Dim arg2 As Single = - 3.3F
        Dim arg3 As Double = 5.5
        Dim opt4 As String = "First optional argument."
        Dim opt5 As Char = "X"c
        Dim opt6 As Boolean = True
        
' Display 4 mandatory and 3 optional arguments using the 
' Console.WriteLine method.
        Console.WriteLine("--- Console.WriteLine ---")
        Console.WriteLine("Mandatory arguments:" & vbCrLf & _
                          "argument 0: {0}" & vbCrLf & _
                          "argument 1: {1}" & vbCrLf & _
                          "argument 2: {2}" & vbCrLf & _
                          "argument 3: {3}" & vbCrLf & vbCrLf & _
                          "Optional arguments:" & vbCrLf & _
                          "optional 4: {4}" & vbCrLf & _
                          "optional 5: {5}" & vbCrLf & _
                          "optional 6: {6}" & vbCrLf, _
                          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:" & vbCrLf & _
                          "argument 0: {0}" & vbCrLf & _
                          "argument 1: {1}" & vbCrLf & _
                          "argument 2: {2}" & vbCrLf & _
                          "argument 3: {3}" & vbCrLf & vbCrLf & _
                          "Optional arguments:" & vbCrLf & _
                          "optional 4: {4}" & vbCrLf & _
                          "optional 5: {5}" & vbCrLf & _
                          "optional 6: {6}" & vbCrLf, _
                          arg0, arg1, arg2, arg3, _
                          opt4, opt5, opt6)
        Console.WriteLine()
    
    End Sub 'Main
End Class 'Sample
'
'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 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft