public sealed class Console
|
Data from the console is read from the standard input stream; normal data to the console is written to the standard output stream; and error data to the console is written to the standard error output stream. These streams are automatically associated with the console when your application starts, and are presented to you as the Console.In, Console.Out, and Console.Error properties.
By default, the value of the Console.In property is a TextReader, while the values of the Console.Out and Console.Error properties are TextWriter objects. However, you can associate these properties with streams that do not represent the console; for example, you can associate them with streams that represent files. You can also associate these properties with different streams at the same time.
This class uses synchronized TextReader and TextWriter instances. Multiple threads can concurrently read from or write to an instance of this type.
This class contains methods that read individual characters or entire lines from the console. This class also contains several write methods that automatically convert individual instances of value types, arrays of characters, or sets of objects to a formatted or unformatted string, then writes that string to the console, optionally followed by a line termination string.
You can also explicitly invoke the method and property members of the stream objects represented by the Console.In, Console.Out, and Console.Error properties. For example, the Console.WriteLine method writes data to the standard output stream followed by a line termination string; that is, data followed by a carriage return and line feed ("\r\n"). However, there is no convenient method to write data to the standard error output stream. Instead, use an explicit invocation like the C# statement,
Console.Error.WriteLine();
.
To continue the previous example, the default value of the line termination string is a carriage return followed by a line feed. Change the line termination character sequence by setting the TextWriter.NewLine property of the Console.Out or Console.Error property to another string. For example, the C# statement,
Console.Error.NewLine = "\r\n\r\n";
, sets the line termination character sequence for the Console.Error.WriteLine method to two carriage return and line feed sequences.
public static void Main(string[] args) { Console.Write("Hola "); Console.WriteLine("Mundo!"); Console.WriteLine("What is your name: "); String name = Console.ReadLine(); Console.Write("Buenos Dias, "); Console.Write(name); Console.WriteLine("!"); }
Error | Read-only Gets the standard error output stream. |
In | Read-only |
Out | Read-only Gets the standard output stream. |
Equals (inherited from System.Object) |
See base class member description: System.Object.Equals Derived from System.Object, the primary base class for all objects. |
GetHashCode (inherited from System.Object) |
See base class member description: System.Object.GetHashCode Derived from System.Object, the primary base class for all objects. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
OpenStandardError | Overloaded:OpenStandardError() Acquires the standard error stream. |
OpenStandardError | Overloaded:OpenStandardError(int bufferSize) Acquires the standard error stream, set to a specified buffer size. |
OpenStandardInput | Overloaded:OpenStandardInput() Acquires the standard input stream. |
OpenStandardInput | Overloaded:OpenStandardInput(int bufferSize) Acquires the standard input stream, set to a specified buffer size. |
OpenStandardOutput | Overloaded:OpenStandardOutput() Acquires the standard output stream. |
OpenStandardOutput | Overloaded:OpenStandardOutput(int bufferSize) Acquires the standard output stream, set to a specified buffer size. |
Read | Reads the next character from the standard input stream. |
ReadLine | Reads the next line of characters from the standard input stream. |
SetError | Sets the Console.Error property to the specified TextWriter. |
SetIn | Sets the Console.In property to the specified TextReader. |
SetOut | Sets the Console.Out property to the specified TextWriter. |
ToString (inherited from System.Object) |
See base class member description: System.Object.ToString Derived from System.Object, the primary base class for all objects. |
Write | Overloaded:Write(bool value) Writes the text representation of the specified Boolean value to the standard output stream. |
Write | Overloaded:Write(char value) Writes the specified Unicode character value to the standard output stream. |
Write | Overloaded:Write(char[] buffer) Writes the specified array of Unicode characters to the standard output stream. |
Write | Overloaded:Write(decimal value) Writes the text representation of the specified Decimal value to the standard output stream. |
Write | Overloaded:Write(double value) Writes the text representation of the specified double-precision floating-point value to the standard output stream. |
Write | Overloaded:Write(int value) Writes the text representation of the specified 32-bit signed integer value to the standard output stream. |
Write | Overloaded:Write(long value) Writes the text representation of the specified 64-bit signed integer value to the standard output stream. |
Write | Overloaded:Write(object value) Writes the text representation of the specified object to the standard output stream. |
Write | Overloaded:Write(float value) Writes the text representation of the specified single-precision floating-point value to the standard output stream. |
Write | Overloaded:Write(string value) Writes the specified string value to the standard output stream. |
Write | Overloaded:Write(uint value) Writes the text representation of the specified 32-bit unsigned integer value to the standard output stream. |
Write | Overloaded:Write(ulong value) Writes the text representation of the specified 64-bit unsigned integer value to the standard output stream. |
Write | Overloaded:Write(string format, object arg0) Writes the specified object to the standard output stream using the specified format information. |
Write | Overloaded:Write(string format, params object[] arg) Writes the specified array of objects to the standard output stream using the specified format information. |
Write | Overloaded:Write(char[] buffer, int index, int count) Writes the specified subarray of Unicode characters to the standard output stream. |
Write | Overloaded:Write(string format, object arg0, object arg1) Writes the specified objects to the standard output stream using the specified format information. |
Write | Overloaded:Write(string format, object arg0, object arg1, object arg2) Writes the specified objects to the standard output stream using the specified format information. |
Write | Overloaded:Write Writes the specified objects and variable length parameter list to the standard output stream using the specified format information. |
WriteLine | Overloaded:WriteLine() Writes the current line terminator to the standard output stream. |
WriteLine | Overloaded:WriteLine(bool value) Writes the text representation of the specified Boolean value, followed by the current line terminator, to the standard output stream. |
WriteLine | Overloaded:WriteLine(char value) Writes the specified Unicode character, followed by the current line terminator, value to the standard output stream. |
WriteLine | Overloaded:WriteLine(char[] buffer) Writes the specified array of Unicode characters, followed by the current line terminator, to the standard output stream. |
WriteLine | Overloaded:WriteLine(decimal value) Writes the text representation of the specified Decimal value, followed by the current line terminator, to the standard output stream. |
WriteLine | Overloaded:WriteLine(double value) Writes the text representation of the specified double-precision floating-point value, followed by the current line terminator, to the standard output stream. |
WriteLine | Overloaded:WriteLine(int value) Writes the text representation of the specified 32-bit signed integer value, followed by the current line terminator, to the standard output stream. |
WriteLine | Overloaded:WriteLine(long value) Writes the text representation of the specified 64-bit signed integer value, followed by the current line terminator, to the standard output stream. |
WriteLine | Overloaded:WriteLine(object value) Writes the text representation of the specified object, followed by the current line terminator, to the standard output stream. |
WriteLine | Overloaded:WriteLine(float value) Writes the text representation of the specified single-precision floating-point value, followed by the current line terminator, to the standard output stream. |
WriteLine | Overloaded:WriteLine(string value) Writes the specified string value, followed by the current line terminator, to the standard output stream. |
WriteLine | Overloaded:WriteLine(uint value) Writes the text representation of the specified 32-bit unsigned integer value, followed by the current line terminator, to the standard output stream. |
WriteLine | Overloaded:WriteLine(ulong value) Writes the text representation of the specified 64-bit unsigned integer value, followed by the current line terminator, to the standard output stream. |
WriteLine | Overloaded:WriteLine(string format, object arg0) Writes the specified object, followed by the current line terminator, to the standard output stream using the specified format information. |
WriteLine | Overloaded:WriteLine(string format, params object[] arg) Writes the specified array of objects, followed by the current line terminator, to the standard output stream using the specified format information. |
WriteLine | Overloaded:WriteLine(char[] buffer, int index, int count) Writes the specified subarray of Unicode characters, followed by the current line terminator, to the standard output stream. |
WriteLine | Overloaded:WriteLine(string format, object arg0, object arg1) Writes the specified objects, followed by the current line terminator, to the standard output stream using the specified format information. |
WriteLine | Overloaded:WriteLine(string format, object arg0, object arg1, object arg2) Writes the specified objects, followed by the current line terminator, to the standard output stream using the specified format information. |
WriteLine | Overloaded:WriteLine Writes the specified objects and variable length parameter list, followed by the current line terminator, to the standard output stream using the specified format information. |
Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
Hierarchy:
public static TextWriter Error {get;}
|
public static TextReader In {get;}
|
public static TextWriter Out {get;}
|
~Console(); |
public virtual int GetHashCode(); |
public Type GetType(); |
protected object MemberwiseClone(); |
public static Stream OpenStandardError(); |
bufferSize
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | bufferSize is less than or equal to zero. |
public static Stream OpenStandardInput(); |
bufferSize
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | bufferSize is less than or equal to zero. |
public static Stream OpenStandardOutput(); |
bufferSize
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | bufferSize is less than or equal to zero. |
public static int Read(); |
Exception Type | Condition |
---|---|
IOException | An I/O error occurred. |
int i; char c; while (true) { i = Console.Read (); if (i == -1) break; c = (char) i; Console.WriteLine ("Echo: {0}", c); } Console.WriteLine ("Done"); return 0;
public static string ReadLine(); |
Exception Type | Condition |
---|---|
IOException | An I/O error occurred. |
OutOfMemoryException | There is insufficient memory to allocate a buffer for the returned string. |
If this method throws OutOfMemoryException, the reader's position in the underlying Stream is advanced by the number of characters the method was able to read, but the characters already read into the internal Console.ReadLine buffer are discarded. Since the position of the reader in the stream cannot be changed, the characters already read are unrecoverable, and can be accessed only by reinitializing the TextReader. If the initial position within the stream is unknown or the stream does not support seeking, the underlying Stream also needs to be reinitialized.
To avoid such a situation and produce robust code you should use the Console.Read method and store the read characters in a preallocated buffer.
public static void SetError( |
newError
Exception Type | Condition |
---|---|
ArgumentNullException | newError is null. |
A StreamWriter that encapsulates a FileStream can be used to send error messages to a file.
public static void SetIn( |
newIn
Exception Type | Condition |
---|---|
ArgumentNullException | newIn is null. |
A StreamReader that encapsulates a FileStream can be used to receive input from a file.
public static void SetOut( |
newOut
Exception Type | Condition |
---|---|
ArgumentNullException | newOut is null. |
A StreamWriter that encapsulates a FileStream can be used to send output to a file. For example:
Console.WriteLine("Hello World"); FileStream fs1 = new FileStream("Test.txt", FileMode.Create); // First, save the standard output. TextWriter tmp = Console.Out; StreamWriter sw1 = new StreamWriter(fs1); Console.SetOut(sw1); Console.WriteLine("Hello file"); Console.SetOut(tmp); Console.WriteLine("Hello World"); sw1.Close();
public virtual string ToString(); |
public static void Write( |
value
Exception Type | Condition |
---|---|
IOException | An I/O error occurred. |
public static void Write( |
value
Exception Type | Condition |
---|---|
IOException | An I/O error occurred. |
public static void Write( |
buffer
Exception Type | Condition |
---|---|
IOException | An I/O error occurred. |
public static void Write( |
value
Exception Type | Condition |
---|---|
IOException | An I/O error occurred. |
public static void Write( |
value
Exception Type | Condition |
---|---|
IOException | An I/O error occurred. |
public static void Write( |
value
Exception Type | Condition |
---|---|
IOException | An I/O error occurred. |
public static void Write( |
value
Exception Type | Condition |
---|---|
IOException | An I/O error occurred. |
public static void Write( |
value
Exception Type | Condition |
---|---|
IOException | An I/O error occurred. |
public static void Write( |
value
Exception Type | Condition |
---|---|
IOException | An I/O error occurred. |
public static void Write( |
value
Exception Type | Condition |
---|---|
IOException | An I/O error occurred. |
[CLSCompliant(false)] |
value
Exception Type | Condition |
---|---|
IOException | An I/O error occurred. |
[CLSCompliant(false)] |
value
Exception Type | Condition |
---|---|
IOException | An I/O error occurred. |
format
arg0
Exception Type | Condition |
---|---|
IOException | An I/O error occurred. |
ArgumentNullException | format or any of the object parameters is null. |
FormatException | The format specification in format is invalid. |
If a parameter object is not referenced in format, it is ignored.
format
arg
Exception Type | Condition |
---|---|
IOException | An I/O error occurred. |
ArgumentNullException | format or any of the object parameters is null. |
FormatException | The format specification in format is invalid. |
If a parameter object is not referenced in format, it is ignored.
buffer
index
count
Exception Type | Condition |
---|---|
ArgumentNullException | buffer is null. |
ArgumentOutOfRangeException | index or count is less than zero. |
ArgumentException | index plus count specify a position that is not within buffer. |
IOException | An I/O error occurred. |
format
arg0
arg1
Exception Type | Condition |
---|---|
IOException | An I/O error occurred. |
ArgumentNullException | format or any of the object parameters is null. |
FormatException | The format specification in format is invalid. |
If a parameter object is not referenced in format, it is ignored.
format
arg0
arg1
arg2
Exception Type | Condition |
---|---|
IOException | An I/O error occurred. |
ArgumentNullException | format or any of the object parameters is null. |
FormatException | The format specification in format is invalid. |
If a parameter object is not referenced in format, it is ignored.
format
arg0
arg1
arg2
arg3
Exception Type | Condition |
---|---|
IOException | An I/O error occurred. |
ArgumentNullException | format or any of the object parameters is null. |
FormatException | The format specification in format is invalid. |
If a parameter object is not referenced in format, it is ignored.
public static void WriteLine(); |
Exception Type | Condition |
---|---|
IOException | An I/O error occurred. |
For example, in C#, set the line terminator to two carriage return and line feed sequences with the statement,
Console.Out.NewLine =
"\r\n\r\n";
.
public static void WriteLine( |
value
Exception Type | Condition |
---|---|
IOException | An I/O error occurred. |
For more information about the line terminator, see the Remarks section of the Console.WriteLine method that takes no parameters.
public static void WriteLine( |
value
Exception Type | Condition |
---|---|
IOException | An I/O error occurred. |
public static void WriteLine( |
buffer
Exception Type | Condition |
---|---|
IOException | An I/O error occurred. |
public static void WriteLine( |
value
Exception Type | Condition |
---|---|
IOException | An I/O error occurred. |
For more information about the line terminator, see the Remarks section of the Console.WriteLine method that takes no parameters.
public static void WriteLine( |
value
Exception Type | Condition |
---|---|
IOException | An I/O error occurred. |
For more information about the line terminator, see the Remarks section of the Console.WriteLine method that takes no parameters.
public static void WriteLine( |
value
Exception Type | Condition |
---|---|
IOException | An I/O error occurred. |
For more information about the line terminator, see the Remarks section of the Console.WriteLine method that takes no parameters.
public static void WriteLine( |
value
Exception Type | Condition |
---|---|
IOException | An I/O error occurred. |
For more information about the line terminator, see the Remarks section of the Console.WriteLine method that takes no parameters.
public static void WriteLine( |
value
Exception Type | Condition |
---|---|
IOException | An I/O error occurred. |
For more information about the line terminator, see the Remarks section of the Console.WriteLine method that takes no parameters.
public static void WriteLine( |
value
Exception Type | Condition |
---|---|
IOException | An I/O error occurred. |
For more information about the line terminator, see the Remarks section of the Console.WriteLine method that takes no parameters.
public static void WriteLine( |
value
Exception Type | Condition |
---|---|
IOException | An I/O error occurred. |
For more information about the line terminator, see the Remarks section of the Console.WriteLine method that takes no parameters.
[CLSCompliant(false)] |
value
Exception Type | Condition |
---|---|
IOException | An I/O error occurred. |
For more information about the line terminator, see the Remarks section of the Console.WriteLine method that takes no parameters.
[CLSCompliant(false)] |
value
Exception Type | Condition |
---|---|
IOException | An I/O error occurred. |
For more information about the line terminator, see the Remarks section of the Console.WriteLine method that takes no parameters.
format
arg0
Exception Type | Condition |
---|---|
IOException | An I/O error occurred. |
ArgumentNullException | format or any of the object parameters is null. |
FormatException | The format specification in format is invalid. |
If a parameter object is not referenced in format, it is ignored.
For more information about the line terminator, see the Remarks section of the Console.WriteLine method that takes no parameters.
format
arg
Exception Type | Condition |
---|---|
IOException | An I/O error occurred. |
ArgumentNullException | format or any of the object parameters is null. |
FormatException | The format specification in format is invalid. |
If a parameter object is not referenced in format, it is ignored.
For more information about the line terminator, see the Remarks section of the Console.WriteLine method that takes no parameters.
buffer
index
count
Exception Type | Condition |
---|---|
ArgumentNullException | buffer is null. |
ArgumentOutOfRangeException | index or count is less than zero. |
ArgumentException | index plus count specify a position that is not within buffer. |
IOException | An I/O error occurred. |
For more information about the line terminator, see the Remarks section of the Console.WriteLine method that takes no parameters.
format
arg0
arg1
Exception Type | Condition |
---|---|
IOException | An I/O error occurred. |
ArgumentNullException | format or any of the object parameters is null. |
FormatException | The format specification in format is invalid. |
If a parameter object is not referenced in format, it is ignored.
For more information about the line terminator, see the Remarks section of the Console.WriteLine method that takes no parameters.
format
arg0
arg1
arg2
Exception Type | Condition |
---|---|
IOException | An I/O error occurred. |
ArgumentNullException | format or any of the object parameters is null. |
FormatException | The format specification in format is invalid. |
If a parameter object is not referenced in format, it is ignored.
For more information about the line terminator, see the Remarks section of the Console.WriteLine method that takes no parameters.
format
arg0
arg1
arg2
arg3
Exception Type | Condition |
---|---|
IOException | An I/O error occurred. |
ArgumentNullException | format or any of the object parameters is null. |
FormatException | The format specification in format is invalid. |
If a parameter object is not referenced in format, it is ignored.
For more information about the line terminator, see the Remarks section of the Console.WriteLine method that takes no parameters.