Export (0) Print
Expand All

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

.NET Framework 1.1

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

This method is not CLS-compliant. For more information about CLS compliance, see What is the Common Language Specification.

[C++]
[CLSCompliant(false)]
public: static void Write(
 String* format,
 Object* arg0,
 Object* arg1,
 Object* arg2,
 Object* arg3,
   ...
);

[Visual Basic] This method cannot be used in Visual Basic.

[C#] This method cannot be used in C#.

[JScript] This method cannot be used in JScript.

Parameters [C++]

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.

Exceptions [C++]

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 [C++]

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 [C++]

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

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

Show:
© 2014 Microsoft