Write Method (String, Object[])

Console.Write Method (String, Object())

 

Writes the text representation of the specified array of objects to the standard output stream using the specified format information.

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

<HostProtectionAttribute(SecurityAction.LinkDemand, UI := True)>
Public Shared Sub Write (
	format As String,
	ParamArray arg As Object()
)

Parameters

format
Type: System.String

A composite format string (see Remarks).

arg
Type: System.Object()

An array of objects to write using format.

Exception Condition
IOException

An I/O error occurred.

ArgumentNullException

format or arg is null.

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 arg parameter is a parameter array. Arguments can be passed to the method either as an array or as list of five or more items. The examples illustrate both forms of method call.

Notes to Callers:

This method is not called by C++ code. The C++ compiler resolves calls to Write that include a string and a list of four or more object parameters as a call to Write(String, Object, Object, Object, Object). It resolves calls to Write that include a string and an object array as a call to Write(String, Object).

The following example defines a Person class with a number of properties that provide information about a person. Its GetDescription method returns an array that contains all of the property values except one. The example then uses the array returned by the GetDescription method to display the values of the Person object.

Public Class Person
   Public Property Name As String
   Public Property BirthDate As DateTime
   Public Property Height As Double
   Public Property Weight As Double
   Public Property Gender As Char
   Public Property Remarks As String

   Public Function GetDescription() As Object()
      Return { Name, Gender, Height, Weight, BirthDate}
   End Function
End Class

Module Example
   Public Sub Main()
      Dim p1 As New Person() With { .Name = "John", .Gender = "M"c,
                                    .BirthDate = New DateTime(1992, 5, 10), 
                                    .Height = 73.5, .Weight = 207 }
      p1.Remarks = "Client since 1/3/2012"
      Console.Write("{0}: {1}, born {4:d}  Height {2} inches, Weight {3} lbs  ", 
                    p1.GetDescription())
      If String.IsNullOrEmpty(p1.Remarks) Then
         Console.WriteLine()
      Else
         Console.WriteLine("{1}Remarks: {0}", p1.Remarks,
                           If(Console.CursorLeft + p1.Remarks.Length + 10 > Console.WindowWidth,
                              vbCrLf + "   ", ""))
      End If   
   End Sub
End Module
' The example displays the following output:
'   John: M, born 5/10/1992  Height 73.5 inches, Weight 207 lbs  Remarks: Client since 1/3/2012

Note that the example calls the Write(String, Object()) method rather than the WriteLine(String, Object()) method because it attempts to display the value of the Person.Remarks property on the same line. To do this, it examines the value of the CursorLeft and WindowWidth properties to determine whether there is enough space for the remark to fit. If there is, it displays the line. If not, it writes a line, indents three spaces, and displays the remark.

The following example is identical to the first, except that it supplies a five-item list as the arg argument instead of a parameter array.

Public Class Person
   Public Property Name As String
   Public Property BirthDate As DateTime
   Public Property Height As Double
   Public Property Weight As Double
   Public Property Gender As Char
   Public Property Remarks As String

   Public Function GetDescription() As Object()
      Return { Name, Gender, Height, Weight, BirthDate}
   End Function
End Class

Module Example
   Public Sub Main()
      Dim p1 As New Person() With { .Name = "John", .Gender = "M"c,
                                    .BirthDate = New DateTime(1992, 5, 10), 
                                    .Height = 73.5, .Weight = 207 }
      p1.Remarks = "Client since 1/3/2012"
      Console.Write("{0}: {1}, born {2:d}  Height {3} inches, Weight {4} lbs  ", 
                    p1.Name, p1.Gender, p1.BirthDate, p1.Height, p1.Weight)
      If String.IsNullOrEmpty(p1.Remarks) Then
         Console.WriteLine()
      Else
         Console.WriteLine("{1}Remarks: {0}", p1.Remarks,
                           If(Console.CursorLeft + p1.Remarks.Length + 10 > Console.WindowWidth,
                              vbCrLf + "   ", ""))
      End If   
   End Sub
End Module
' The example displays the following output:
'   John: M, born 5/10/1992  Height 73.5 inches, Weight 207 lbs  Remarks: Client since 1/3/2012

UIPermission

for modifying safe top-level windows and subwindows. Associated enumeration: UIPermissionWindow.SafeTopLevelWindows

Universal Windows Platform
Available since 10
.NET Framework
Available since 1.1
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Return to top
Show:
© 2016 Microsoft