8 out of 15 rated this helpful - Rate this topic

Console Class

Represents the standard input, output, and error streams for console applications. This class cannot be inherited.

System.Object
  System.Console

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
public static class Console

The Console type exposes the following members.

  Name Description
Public property Static member BackgroundColor Gets or sets the background color of the console.
Public property Static member BufferHeight Gets or sets the height of the buffer area.
Public property Static member BufferWidth Gets or sets the width of the buffer area.
Public property Static member CapsLock Gets a value indicating whether the CAPS LOCK keyboard toggle is turned on or turned off.
Public property Static member CursorLeft Gets or sets the column position of the cursor within the buffer area.
Public property Static member CursorSize Gets or sets the height of the cursor within a character cell.
Public property Static member CursorTop Gets or sets the row position of the cursor within the buffer area.
Public property Static member CursorVisible Gets or sets a value indicating whether the cursor is visible.
Public property Static member Supported by the XNA Framework Error Gets the standard error output stream.
Public property Static member ForegroundColor Gets or sets the foreground color of the console.
Public property Static member Supported by the XNA Framework In Gets the standard input stream.
Public property Static member InputEncoding Gets or sets the encoding the console uses to read input.
Public property Static member KeyAvailable Gets a value indicating whether a key press is available in the input stream.
Public property Static member LargestWindowHeight Gets the largest possible number of console window rows, based on the current font and screen resolution.
Public property Static member LargestWindowWidth Gets the largest possible number of console window columns, based on the current font and screen resolution.
Public property Static member NumberLock Gets a value indicating whether the NUM LOCK keyboard toggle is turned on or turned off.
Public property Static member Supported by the XNA Framework Out Gets the standard output stream.
Public property Static member OutputEncoding Gets or sets the encoding the console uses to write output.
Public property Static member Title Gets or sets the title to display in the console title bar.
Public property Static member TreatControlCAsInput Gets or sets a value indicating whether the combination of the Control modifier key and C console key (CTRL+C) is treated as ordinary input or as an interruption that is handled by the operating system.
Public property Static member WindowHeight Gets or sets the height of the console window area.
Public property Static member WindowLeft Gets or sets the leftmost position of the console window area relative to the screen buffer.
Public property Static member WindowTop Gets or sets the top position of the console window area relative to the screen buffer.
Public property Static member WindowWidth Gets or sets the width of the console window.
Top
  Name Description
Public method Static member Beep() Plays the sound of a beep through the console speaker.
Public method Static member Beep(Int32, Int32) Plays the sound of a beep of a specified frequency and duration through the console speaker.
Public method Static member Clear Clears the console buffer and corresponding console window of display information.
Public method Static member MoveBufferArea(Int32, Int32, Int32, Int32, Int32, Int32) Copies a specified source area of the screen buffer to a specified destination area.
Public method Static member MoveBufferArea(Int32, Int32, Int32, Int32, Int32, Int32, Char, ConsoleColor, ConsoleColor) Copies a specified source area of the screen buffer to a specified destination area.
Public method Static member OpenStandardError() Acquires the standard error stream.
Public method Static member OpenStandardError(Int32) Acquires the standard error stream, which is set to a specified buffer size.
Public method Static member OpenStandardInput() Acquires the standard input stream.
Public method Static member OpenStandardInput(Int32) Acquires the standard input stream, which is set to a specified buffer size.
Public method Static member OpenStandardOutput() Acquires the standard output stream.
Public method Static member OpenStandardOutput(Int32) Acquires the standard output stream, which is set to a specified buffer size.
Public method Static member Read Reads the next character from the standard input stream.
Public method Static member ReadKey() Obtains the next character or function key pressed by the user. The pressed key is displayed in the console window.
Public method Static member ReadKey(Boolean) Obtains the next character or function key pressed by the user. The pressed key is optionally displayed in the console window.
Public method Static member Supported by the XNA Framework ReadLine Reads the next line of characters from the standard input stream.
Public method Static member ResetColor Sets the foreground and background console colors to their defaults.
Public method Static member SetBufferSize Sets the height and width of the screen buffer area to the specified values.
Public method Static member SetCursorPosition Sets the position of the cursor.
Public method Static member Supported by the XNA Framework SetError Sets the Error property to the specified TextWriter object.
Public method Static member Supported by the XNA Framework SetIn Sets the In property to the specified TextReader object.
Public method Static member Supported by the XNA Framework SetOut Sets the Out property to the specified TextWriter object.
Public method Static member SetWindowPosition Sets the position of the console window relative to the screen buffer.
Public method Static member SetWindowSize Sets the height and width of the console window to the specified values.
Public method Static member Write(Boolean) Writes the text representation of the specified Boolean value to the standard output stream.
Public method Static member Supported by the XNA Framework Write(Char) Writes the specified Unicode character value to the standard output stream.
Public method Static member Supported by the XNA Framework Write(Char[]) Writes the specified array of Unicode characters to the standard output stream.
Public method Static member Write(Decimal) Writes the text representation of the specified Decimal value to the standard output stream.
Public method Static member Write(Double) Writes the text representation of the specified double-precision floating-point value to the standard output stream.
Public method Static member Supported by the XNA Framework Write(Int32) Writes the text representation of the specified 32-bit signed integer value to the standard output stream.
Public method Static member Write(Int64) Writes the text representation of the specified 64-bit signed integer value to the standard output stream.
Public method Static member Supported by the XNA Framework Write(Object) Writes the text representation of the specified object to the standard output stream.
Public method Static member Write(Single) Writes the text representation of the specified single-precision floating-point value to the standard output stream.
Public method Static member Supported by the XNA Framework Write(String) Writes the specified string value to the standard output stream.
Public method Static member Write(UInt32) Writes the text representation of the specified 32-bit unsigned integer value to the standard output stream.
Public method Static member Write(UInt64) Writes the text representation of the specified 64-bit unsigned integer value to the standard output stream.
Public method Static member Supported by the XNA Framework Write(String, Object) Writes the text representation of the specified object to the standard output stream using the specified format information.
Public method Static member Supported by the XNA Framework Write(String, Object[]) Writes the text representation of the specified array of objects to the standard output stream using the specified format information.
Public method Static member Supported by the XNA Framework Write(Char[], Int32, Int32) Writes the specified subarray of Unicode characters to the standard output stream.
Public method Static member Supported by the XNA Framework Write(String, Object, Object) Writes the text representation of the specified objects to the standard output stream using the specified format information.
Public method Static member Supported by the XNA Framework Write(String, Object, Object, Object) Writes the text representation of the specified objects to the standard output stream using the specified format information.
Public method Static member Write(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.
Public method Static member Supported by the XNA Framework WriteLine() Writes the current line terminator to the standard output stream.
Public method Static member WriteLine(Boolean) Writes the text representation of the specified Boolean value, followed by the current line terminator, to the standard output stream.
Public method Static member Supported by the XNA Framework WriteLine(Char) Writes the specified Unicode character, followed by the current line terminator, value to the standard output stream.
Public method Static member Supported by the XNA Framework WriteLine(Char[]) Writes the specified array of Unicode characters, followed by the current line terminator, to the standard output stream.
Public method Static member WriteLine(Decimal) Writes the text representation of the specified Decimal value, followed by the current line terminator, to the standard output stream.
Public method Static member WriteLine(Double) Writes the text representation of the specified double-precision floating-point value, followed by the current line terminator, to the standard output stream.
Public method Static member Supported by the XNA Framework WriteLine(Int32) Writes the text representation of the specified 32-bit signed integer value, followed by the current line terminator, to the standard output stream.
Public method Static member WriteLine(Int64) Writes the text representation of the specified 64-bit signed integer value, followed by the current line terminator, to the standard output stream.
Public method Static member Supported by the XNA Framework WriteLine(Object) Writes the text representation of the specified object, followed by the current line terminator, to the standard output stream.
Public method Static member WriteLine(Single) Writes the text representation of the specified single-precision floating-point value, followed by the current line terminator, to the standard output stream.
Public method Static member Supported by the XNA Framework WriteLine(String) Writes the specified string value, followed by the current line terminator, to the standard output stream.
Public method Static member WriteLine(UInt32) Writes the text representation of the specified 32-bit unsigned integer value, followed by the current line terminator, to the standard output stream.
Public method Static member WriteLine(UInt64) Writes the text representation of the specified 64-bit unsigned integer value, followed by the current line terminator, to the standard output stream.
Public method Static member Supported by the XNA Framework WriteLine(String, Object) Writes the text representation of the specified object, followed by the current line terminator, to the standard output stream using the specified format information.
Public method Static member Supported by the XNA Framework WriteLine(String, Object[]) Writes the text representation of the specified array of objects, followed by the current line terminator, to the standard output stream using the specified format information.
Public method Static member WriteLine(Char[], Int32, Int32) Writes the specified subarray of Unicode characters, followed by the current line terminator, to the standard output stream.
Public method Static member Supported by the XNA Framework WriteLine(String, Object, Object) Writes the text representation of the specified objects, followed by the current line terminator, to the standard output stream using the specified format information.
Public method Static member Supported by the XNA Framework WriteLine(String, Object, Object, Object) Writes the text representation of the specified objects, followed by the current line terminator, to the standard output stream using the specified format information.
Public method Static member WriteLine(String, Object, Object, Object, Object) Writes the text representation of the specified objects and variable-length parameter list, followed by the current line terminator, to the standard output stream using the specified format information.
Top
  Name Description
Public event Static member CancelKeyPress Occurs when the Control modifier key (CTRL) and C console key (C) are pressed simultaneously (CTRL+C).
Top

The console is an operating system window where users interact with the operating system or a text-based console application by entering text input through the computer keyboard, and reading text output from the computer terminal. For example, in Windows the console is called the command prompt window and accepts MS-DOS commands. The Console class provides basic support for applications that read characters from, and write characters to, the console.

Console I/O Streams

When a console application starts, the operating system automatically associates three I/O streams with the console. Your application can read user input from the standard input stream; write normal data to the standard output stream; and write error data to the standard error output stream. These streams are presented to your application as the values of the In, Out, and Error properties.

By default, the value of the In property is a System.IO.TextReader object, and the values of the Out and Error properties are System.IO.TextWriter objects. However, you can set these properties to streams that do not represent the console; for example, you can set these properties to streams that represent files. To redirect the standard input, standard output, or standard error stream, call the SetIn, SetOut, or SetError method, respectively. I/O operations using these streams are synchronized, which means multiple threads can read from, or write to, the streams.

Note Note

You should not use the Console class to display output in unattended applications, such as server applications. Similarly, calls to methods such as Write and WriteLine have no effect in Windows applications.

Console class members that work normally when the underlying stream is directed to a console might throw an exception if the stream is redirected, for example, to a file. Consequently, program your application to catch System.IO.IOException if you redirect a standard stream.

It is sometimes useful to explicitly invoke the members of the stream objects represented by the In, Out, and Error properties. For example, by default, the Console.ReadLine method reads input from the standard input stream. Similarly, the Console.WriteLine method writes data to the standard output stream followed by the default line termination string; that is, data is followed by a carriage return and line feed ("\r\n"). However, the Console class does not provide a corresponding method to write data to the standard error output stream, or a property to change the line termination string for data written to that stream.

You can solve this problem by setting the TextWriter.NewLine property of the Out or Error property to another line termination string. For example, the C# statement, Console.Error.NewLine = "\r\n\r\n";, sets the line termination string for the standard error output stream to two carriage return and line feed sequences. Then you can explicitly call the WriteLine method of the error output stream object, as in the C# statement, Console.Error.WriteLine();.

Screen Buffer and Console Window

Two closely related features of the console are the screen buffer and the console window. Text is actually read from or written to streams owned by the console, but appear to be read from or written to an area owned by the console called the screen buffer. The screen buffer is an attribute of the console, and is organized as a rectangular grid of rows and columns where each grid intersection, or character cell, can contain a character. Each character has its own foreground color and each character cell has its own background color.

The screen buffer is viewed through a rectangular region called the console window. The console window is another attribute of the console; it is not the console itself, which is an operating system window. The console window is also arranged in rows and columns, is less than or equal to the size of the screen buffer, and can be moved to view different areas of the underlying screen buffer. If the screen buffer is larger than the console window, the console automatically displays scroll bars so the console window can be repositioned over the screen buffer area.

A cursor indicates the screen buffer position where text is currently read or written. The cursor can be hidden or made visible, and its height can be changed. If the cursor is visible, the console window position is moved automatically so the cursor is always in view.

The origin for character cell coordinates in the screen buffer is the upper left corner, and the position of the cursor and the console window are measured relative to that origin. Use zero-based indexes to specify positions; that is, specify the topmost row as row 0, and the leftmost column as column 0. The maximum value for the row and column indexes is Int16.MaxValue.

Functionality

The Console class contains methods that read individual characters or entire lines from the console; and several write methods that automatically convert an instance of a value type, an array of characters, or sets of objects to a formatted or unformatted string, and then writes that string, optionally followed by a line termination string, to the console. The Console class also contains methods and properties to get or set the size of the screen buffer, console window, and cursor; to change the position of the console window and cursor; to move or clear data in the screen buffer; to change foreground and background colors; to change the text displayed in the console title bar; and to play the sound of a beep.

The Console class also includes members that support customization of the appearance of the console window itself. You can call the SetWindowSize method to change the number of rows and columns in the console window at the same time, or you can use the WindowHeight and WindowWidth properties to change the number of rows and columns independently of one another. You can use the ForegroundColor and BackgroundColor properties to control the color of the console window, and you can use the CursorSize and CursorVisible properties to customize the cursor in the console window.

This code sample demonstrates how to read from and write to the standard input and output streams. Note that these streams can be redirected using the SetIn and SetOut methods.


using System;

public class Example {
    public static void Main() 
    {
        Console.Write("Hello ");
        Console.WriteLine("World!");
        Console.Write("Enter your name: ");
        String name = Console.ReadLine();
        Console.Write("Good day, ");
        Console.Write(name);
        Console.WriteLine("!");
    }
}
// The example displays output similar to the following:
//       Hello World!
//       Enter your name: James
//       Good day, James!


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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

This type is thread safe.

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ