Console Class

Namespace: System
Assembly: System.Runtime
Provides access to the standard input, output, and error streams for console applications.

Class Members

  • BackgroundColor ConsoleColor
    Sets or gets the background color of the console.
  • ForegroundColor ConsoleColor
    Sets or gets the foreground color of the console.
  • InputEncoding Encoding
    Gets or sets the input encoding of the console.
  • Gets or sets the output encoding of the console.
  • Gets a value that indicates whether the console error stream is redirected.
  • Gets a value that indicates whether the console input stream is redirected.
  • Gets a value that indicates whether the console output stream is redirected.
  • KeyAvailable Boolean
    Gets a value that indicates whether a key press is waiting to be read.
  • Gets or sets the height of the console window.
  • Gets or sets the horizontal position of the console window.
  • WindowTop Int32
    Gets or sets the vertical position of the console window.
  • Gets or sets the width of the console window.
  • WindowState FormWindowState
    Gets or sets the state of the console window (Normal, Maximized, Minimized).
  • Emits a standard beep sound through the console speaker.
  • Emits a beep sound with the specified frequency and duration through the console speaker.
  • Clears the console buffer and positions the cursor at the top-left corner.
  • Gets the number of characters in the console input buffer.
  • Reads the next key or function key pressed by the user and, optionally, displays the key.
  • Reads the next line of characters from the standard input stream and returns them as a string.
  • Reads the next key press.
  • Writes the specified string value to the standard output stream.
  • Writes the specified string value, followed by the current line terminator, to the standard output stream.
  • Writes the formatted string to the standard output stream, followed by the current line terminator.
  • Writes the string representation of the specified object to the standard output stream, followed by the current line terminator.
  • Sets the foreground and background console colors.
  • Sets the position of the cursor to the specified row and column.

BackgroundColor

public ConsoleColor BackgroundColor { get; set; }
Gets or sets the background color of the console.
Remarks
The color is specified by a ConsoleColor enumeration value. The default is Black.

ForegroundColor

public ConsoleColor ForegroundColor { get; set; }
Gets or sets the foreground color of the console.
Remarks
The color is specified by a ConsoleColor enumeration value. The default is Gray.

InputEncoding

public Encoding InputEncoding { get; set; }
Gets or sets the input encoding of the console.
Remarks
The encoding determines which characters can be entered from the keyboard and read by the ReadLine method.

OutputEncoding

public Encoding OutputEncoding { get; set; }
Gets or sets the output encoding of the console.
Remarks
The encoding determines which characters can be displayed in the console.

Beep()

public static void Beep()
Emits a standard beep sound through the console speaker.
Remarks
This method uses the default frequency and duration for the system's beep sound.

Beep(Int32, Int32)

public static void Beep(int frequency, int duration)
Emits a beep sound with the specified frequency and duration through the console speaker.
Parameters
  • frequency: The frequency of the beep in Hertz.
  • duration: The duration of the beep in milliseconds.
Remarks
Valid frequencies are between 37 and 32,767, and valid durations are between 1 and 65,535.

Clear()

public static void Clear()
Clears the console buffer and positions the cursor at the top-left corner.
Remarks
All text in the console is removed. The cursor is moved to position (0,0).

ReadKey(Boolean)

public static ConsoleKeyInfo ReadKey(bool intercept)
Reads the next key or function key pressed by the user and, optionally, displays the key.
Parameters
  • intercept: true to display the key press; otherwise, false.
Returns
A ConsoleKeyInfo object that describes the console key that was pressed.

ReadLine()

public static string ReadLine()
Reads the next line of characters from the standard input stream and returns them as a string.
Returns
The next line from the input stream, or null if the end of the stream is reached.
Remarks
The ReadLine method returns the line of text entered by the user. The line terminator is not included in the returned string.

ReadKey()

public static ConsoleKeyInfo ReadKey()
Reads the next key press.
Returns
A ConsoleKeyInfo object that describes the console key that was pressed.

Write(String)

public static void Write(string value)
Writes the specified string value to the standard output stream.
Parameters
  • value: The string to write.

WriteLine(String)

public static void WriteLine(string value)
Writes the specified string value, followed by the current line terminator, to the standard output stream.
Parameters
  • value: The string to write.

WriteLine(String, Object[])

public static void WriteLine(string format, params object[] arg)
Writes the formatted string to the standard output stream, followed by the current line terminator.
Parameters
  • format: A composite format string that contains format specifiers.
  • arg: An array of objects to format and write.
Remarks
The WriteLine(String, Object[]) method formats using the same syntax as the String.Format method.

WriteLine(Object)

public static void WriteLine(object value)
Writes the string representation of the specified object to the standard output stream, followed by the current line terminator.
Parameters
  • value: The object to write.
Remarks
If value is null, an empty line is written to the console. Otherwise, the string representation of the value is written.

SetColors(ConsoleColor, ConsoleColor)

public static void SetColors(ConsoleColor foreground, ConsoleColor background)
Sets the foreground and background console colors.
Parameters
  • foreground: The foreground color.
  • background: The background color.

SetCursorPosition(Int32, Int32)

public static void SetCursorPosition(int left, int top)
Sets the position of the cursor to the specified row and column.
Parameters
  • left: The column position of the cursor (0-based).
  • top: The row position of the cursor (0-based).
Remarks
The cursor position is relative to the console window's upper-left corner.