C# Console Class
In C#, the Console class is used to represent the standard input, output, and error streams for console applications. It is defined under the System namespace and does not contain any constructors. Instead of a constructor, this class provides various properties and methods to perform different operations.
A console is an operating system window through which a user can communicate with the system. In simple terms, a console application allows users to input text from the keyboard and receive text-based output from the program. The Command Prompt in Windows is an example of a console.
The console has two main components:
- Screen Buffer: Stores text output before displaying it on the console window.
- Console Window: Displays the visible output of the console application.
Example:
// Demonstration of Console class methods
using System;
class Geeks
{
static void Main()
{
// Using Write method to print
// text without a new line
Console.Write("Hello, ");
// Using WriteLine method to
// print text with a new line
Console.WriteLine("From GeeksforGeeks!");
}
}
Output
Hello, From GeeksforGeeks!
Properties
Properties | Description |
---|---|
Gets or sets the background colour of the console. | |
Gets or sets the height of the buffer area. | |
Gets or sets the width of the buffer area. | |
Gets a value indicating whether the CAPS LOCK keyboard toggle is turned on or turned off. | |
Gets or sets the column position of the cursor within the buffer area. | |
Gets or sets the height of the cursor within a character cell. | |
Gets or sets the row position of the cursor within the buffer area. | |
Gets or sets a value indicating whether the cursor is visible. | |
Gets the standard error output stream. | |
Gets or sets the foreground colour of the console. | |
Gets the standard input stream. | |
Gets or sets the encoding the console uses to read input. | |
Gets a value that indicates whether the error output stream has been redirected from the standard error stream. | |
Gets a value that indicates whether input has been redirected from the standard input stream. | |
Gets a value that indicates whether output has been redirected from the standard output stream. | |
Gets a value indicating whether a key press is available in the input stream. | |
Gets the largest possible number of console window rows, based on the current font and screen resolution. | |
Gets the largest possible number of console window columns, based on the current font and screen resolution. | |
Gets a value indicating whether the NUM LOCK keyboard toggle is turned on or turned off. | |
Gets the standard output stream. | |
Gets or sets the encoding the console uses to write output. | |
Gets or sets the title to display in the console title bar. | |
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. | |
Gets or sets the height of the console window area. | |
Gets or sets the leftmost position of the console window area relative to the screen buffer. | |
Gets or sets the top position of the console window area relative to the screen buffer. | |
Gets or sets the width of the console window. |
Example: Getting Console Colors
// C# program to get and set console colors
using System;
public class Geeks
{
static public void Main()
{
// Display the current background and foreground colors
Console.WriteLine("Background color : {0}"
, Console.BackgroundColor);
Console.WriteLine("Foreground color : {0}"
, Console.ForegroundColor);
}
}
Output:

Methods
Method | Description |
---|---|
Plays the sound of a beep through the console speaker. | |
Clears the console buffer and corresponding console window of display information. | |
Copies a specified source area of the screen buffer to a specified destination area. | |
OpenStandardError() | Acquires the standard error stream. |
Acquires the standard input stream. | |
OpenStandardOutput() | Acquires the standard output stream. |
Reads the next character from the standard input stream. | |
Obtains the next character or function key pressed by the user. The pressed key is displayed in the console window. | |
Reads the next line of characters from the standard input stream. | |
Sets the foreground and background console colours to their defaults. | |
Sets the height and width of the screen buffer area to the specified values. | |
Sets the position of the cursor. | |
Sets the Error property to the specified TextWriter object. | |
Sets the In property to the specified TextReader object. | |
Sets the Out property to the specified TextWriter object. | |
Sets the position of the console window relative to the screen buffer. | |
Sets the height and width of the console window to the specified values. | |
Write() | Writes the text representation of the specified value or values to the standard output stream. |
WriteLine() | Writes the specified data, followed by the current line terminator, to the standard output stream. |
Example: Using WriteLine() Method
// C# program demonstrating WriteLine method
using System;
public class Geeks
{
static public void Main()
{
// Display messages using WriteLine
Console.WriteLine("Welcome to GeeksforGeeks");
Console.WriteLine("This is a tutorial on the Console Class.");
}
}
Output
Welcome to GeeksforGeeks This is a tutorial on the Console Class.
Events
The Console class in C# provides events used to execute certain programs when some event is triggered such as cancelling an operation or modifying console settings dynamically. For Example: CancelKeyPress Event Occurs when the Control modifier key (Ctrl) and either the C console key (C) or the Break key are pressed simultaneously (Ctrl+C or Ctrl+Break).
Example:
// C# program to handle Ctrl+C event
using System;
using System.Threading;
class Geeks
{
static Boolean flag = true;
static void Main()
{
Console.CancelKeyPress += (sender, e) =>
{
Console.WriteLine("\nCTRL+C detected! Exiting Process");
e.Cancel = true;
flag = false;
};
Console.WriteLine("Press CTRL+C to trigger the event.");
while (flag)
{
Console.WriteLine("Programme is running... Press ctrl+C to stop.");
Thread.Sleep(1000);
}
}
}
Output: