Console Class

Provides access to the standard input, output, and error streams for console applications.

Namespace: System

Assembly: System (in System.dll)

Syntax

public static class Console

Summary

The Console class provides a set of methods and properties that allow you to interact with the console window. This includes reading input from the keyboard, writing text to the console, and controlling console appearance like color and cursor position.

Members

Properties

Name Description
BackgroundColor Gets or sets the background color of the console.
BufferHeight Gets or sets the height of the console-mode text buffer.
BufferWidth Gets or sets the width of the console-mode text buffer.
CapsLock Gets a value indicating whether the NUM LOCK key is pressed.
CursorLeft Gets or sets the column position of the cursor within the current row or line.
CursorSize Gets or sets the size of the cursor relative to the height of a character cell.
CursorTop Gets or sets the row position of the cursor.
Error Gets the standard error stream.
ForegroundColor Gets or sets the foreground color of the console text.
InputEncoding Gets or sets the input encoding for characters read from the standard input stream.
IsErrorRedirected Gets a value indicating whether the standard error stream is redirected.
IsInputRedirected Gets a value indicating whether the standard input stream is redirected.
IsOutputRedirected Gets a value indicating whether the standard output stream is redirected.
KeyAvailable Gets a value indicating whether a key press is waiting to be read.
LargestWindowHeight Gets the largest possible width of the console buffer.
LargestWindowWidth Gets the largest possible height of the console buffer.
NumberLock Gets a value indicating whether the CAPS LOCK key is pressed.
OutputEncoding Gets or sets the output encoding for characters written to the standard output stream.
Title Gets or sets the title of the console window.
WindowHeight Gets or sets the height of the console window.
WindowLeft Gets or sets the column position of the console window relative to the right edge of the screen.
WindowTop Gets or sets the row position of the console window relative to the screen.
WindowWidth Gets or sets the width of the console window.

Methods

Name Description
Beep() Emits a standard beep sound using the console.
Beep(int frequency, int duration) Emits a beep sound with a specified frequency and duration.
Clear() Clears the console buffer and the associated display of the console text.
MoveBufferArea(int sourceLeft, int sourceTop, int sourceWidth, int sourceHeight, int targetLeft, int targetTop) Moves a block of text from one location to another within the console buffer.
MoveBufferArea(int sourceLeft, int sourceTop, int sourceWidth, int sourceHeight, int targetLeft, int targetTop, char fill) Moves a block of text and fills the empty space with a specified character.
Pause() Waits for the user to press any key.
Read() Reads the next character from the standard input stream.
ReadAllLines() Reads all lines from the standard input stream and returns them as an array of strings.
ReadAllText() Reads all characters from the standard input stream and returns them as a single string.
ReadLine() Reads the next line of characters from the standard input stream and returns it as a string.
SetBufferSize(int width, int height) Sets the width and height of the console text buffer.
SetCursorPosition(int left, int top) Sets the position of the cursor.
SetWindowPosition(int left, int top) Sets the position of the console window.
SetWindowSize(int width, int height) Sets the width and height of the console window.
Write(string value) Writes the specified value to the standard output stream.
WriteLine(string value) Writes the specified value, followed by the current line terminator, to the standard output stream.

Example

Writing to the Console

This example demonstrates how to write text to the console and change its colors.

using System;

public class ConsoleDemo
{
    public static void Main(string[] args)
    {
        Console.ForegroundColor = ConsoleColor.Yellow;
        Console.WriteLine("Hello, Console!");
        Console.ResetColor();

        Console.WriteLine("This is the default color.");

        Console.WriteLine($"Current cursor position: Left={Console.CursorLeft}, Top={Console.CursorTop}");

        Console.Beep(1000, 500); // Beep with frequency 1000Hz for 500ms
    }
}

Reading from the Console

This example reads user input from the console.

using System;

public class InputDemo
{
    public static void Main(string[] args)
    {
        Console.Write("Please enter your name: ");
        string name = Console.ReadLine();

        Console.WriteLine($"Hello, {name}!");

        Console.Write("Enter a number: ");
        string input = Console.ReadLine();
        if (int.TryParse(input, out int number))
        {
            Console.WriteLine($"Your number is: {number}");
        }
        else
        {
            Console.WriteLine("Invalid input.");
        }
    }
}

Important Note

The Console class is static, meaning you access its members directly using the class name (e.g., Console.WriteLine()) without needing to create an instance of the class.