Export (0) Print
Expand All

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

.NET Framework 1.1

Writes the specified objects to the standard output stream using the specified format information.

[Visual Basic]
Overloads Public Shared Sub Write( _
   ByVal format As String, _
   ByVal arg0 As Object, _
   ByVal arg1 As Object, _
   ByVal arg2 As Object _
)
[C#]
public static void Write(
 string format,
 object arg0,
 object arg1,
 object arg2
);
[C++]
public: static void Write(
 String* format,
 Object* arg0,
 Object* arg1,
 Object* arg2
);
[JScript]
public static function Write(
   format : String,
 arg0 : Object,
 arg1 : Object,
 arg2 : 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.

Exceptions

Exception Type Condition
IOException An I/O error occurred.
ArgumentNullException format is a null reference (Nothing in Visual Basic).
FormatException The format specification in format is invalid.

Remarks

This method uses the same semantics as String.Format.

If a parameter object is not referenced in format, it is ignored.

Example

The following code sample illustrates the use of Write:

[Visual Basic] 
Public Class FormatConverter
   Public Shared Sub Main()
      Dim lineInput As String
      lineInput = Console.ReadLine()
      While Not lineInput Is Nothing
         Dim fields() As String = lineInput.Split(ControlChars.Tab)
         Dim isFirstField As Boolean = True
         Dim item As String
         For Each item In  fields
            If isFirstField Then
               isFirstField = False
            Else
               Console.Write(",")
            End If
            ' If the field represents a boolean, replace with a numeric representation.
            Try
               Console.Write(Convert.ToByte(Convert.ToBoolean(item)))
            Catch
               Console.Write(item)
            End Try
         Next item
         Console.WriteLine()
         lineInput = Console.ReadLine()
      End While
   End Sub 'Main
End Class 'FormatConverter

[C#] 
public class FormatConverter {
    public static void Main(string[] args) {
        string lineInput;
        while ((lineInput = Console.ReadLine()) != null) {
            string[] fields = lineInput.Split(new char[] {'\t'});
            bool isFirstField = true;
            foreach(string item in fields) {
                if (isFirstField)
                    isFirstField = false;
                else
                    Console.Write(',');
                // If the field represents a boolean, replace with a numeric representation.
                try {
                    Console.Write(Convert.ToByte(Convert.ToBoolean(item)));
                }
                catch(FormatException) {
                    Console.Write(item);
                }
            }
            Console.WriteLine();
        }
    }
}

[C++] 

int main() {
   String* lineInputArr __gc[] = {S"1    2.2    hello    TRUE", S"2    5.22    bye    FALSE", S"3    6.38    see ya'    TRUE"};

   for( Int32 i = 0; i < 3; i++) {
      String* lineInput = lineInputArr->GetValue(i)->ToString();
      String* aChar = "\t";
      String* fields __gc[] = lineInput->Split(aChar->ToCharArray());
      Boolean isFirstField = true;
      for (Int32 i = 0; i < fields->Length; i++) {
         if (isFirstField)
            isFirstField = false;
         else
            Console::Write(S",");

         // If the field represents a boolean, replace with a numeric representation.
         try {
            Console::Write(Convert::ToByte(Convert::ToBoolean(fields[i])));
         }
         catch(FormatException*) {
            Console::Write(fields[i]);
         }
      }
      Console::WriteLine();
   }
}


[JScript] 
var lineInput : String;
while ((lineInput = Console.ReadLine()) != null) {
    var fields : String[] = lineInput.Split(char[](['\t']));
    var isFirstField : Boolean = true;
    for(var i in fields) {
        var item = fields[i];
        if (isFirstField)
            isFirstField = false;
        else
            Console.Write(',');
        // If the field represents a boolean, replace with a numeric representation.
        try {
            Console.Write(Convert.ToByte(Convert.ToBoolean(item)));
        }
        catch(FormatException) {
            Console.Write(item);
        }
    }
    Console.WriteLine();
}

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

Console Class | Console Members | System Namespace | Console.Write Overload List | Formatting Overview | Read | ReadLine | WriteLine

Show:
© 2014 Microsoft