Skip to content
Related Articles

Related Articles

Console Class in C#
  • Last Updated : 14 Mar, 2019

A console is an operating system window through which a user can communicate with the operating system or we can say a console is an application in which we can give text as an input from the keyboard and get the text as an output from the computer end. The command prompt is an example of a console in the windows and which accept MS-DOS commands. The console contains two attributes named as screen buffer and a console window.
In C#, the Console class is used to represent the standard input, output, and error streams for the console applications. You are not allowed to inherit Console class. This class is defined under System namespace. This class does not contain any constructor. Instead of the constructor, this class provides different types of properties and methods to perform operations.

Properties

PropertyDescription
BackgroundColorGets or sets the background color of the console.
BufferHeightGets or sets the height of the buffer area.
BufferWidthGets or sets the width of the buffer area.
CapsLockGets a value indicating whether the CAPS LOCK keyboard toggle is turned on or turned off.
CursorLeftGets or sets the column position of the cursor within the buffer area.
CursorSizeGets or sets the height of the cursor within a character cell.
CursorTopGets or sets the row position of the cursor within the buffer area.
CursorVisibleGets or sets a value indicating whether the cursor is visible.
ErrorGets the standard error output stream.
ForegroundColorGets or sets the foreground color of the console.
InGets the standard input stream.
InputEncodingGets or sets the encoding the console uses to read input.
IsErrorRedirectedGets a value that indicates whether the error output stream has been redirected from the standard error stream.
IsInputRedirectedGets a value that indicates whether input has been redirected from the standard input stream.
IsOutputRedirectedGets a value that indicates whether output has been redirected from the standard output stream.
KeyAvailableGets a value indicating whether a key press is available in the input stream.
LargestWindowHeightGets the largest possible number of console window rows, based on the current font and screen resolution.
LargestWindowWidthGets the largest possible number of console window columns, based on the current font and screen resolution.
NumberLockGets a value indicating whether the NUM LOCK keyboard toggle is turned on or turned off.
OutGets the standard output stream.
OutputEncodingGets or sets the encoding the console uses to write output.
TitleGets or sets the title to display in the console title bar.
TreatControlCAsInputGets 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.
WindowHeightGets or sets the height of the console window area.
WindowLeftGets or sets the leftmost position of the console window area relative to the screen buffer.
WindowTopGets or sets the top position of the console window area relative to the screen buffer.
WindowWidthGets or sets the width of the console window.

Example:




// C# program to illustrate how to get
// Background and Foreground color
// of the console
using System;
  
public class GFG {
  
    static public void Main()
    {
  
        // Get the Background and foreground 
        // color of Console Using BackgroundColor
        // and ForegroundColor property of Console
        Console.WriteLine("Background color  :{0}",
                        Console.BackgroundColor);
  
        Console.WriteLine("Foreground color : {0}"
                        Console.ForegroundColor);
    }
}
Output:
Background color : Black
Foreground color : Black

Methods

MethodDescription
Beep()Plays the sound of a beep through the console speaker.
Clear()Clears the console buffer and corresponding console window of display information.
MoveBufferArea()Copies a specified source area of the screen buffer to a specified destination area.
OpenStandardError()Acquires the standard error stream.
OpenStandardInput()Acquires the standard input stream.
OpenStandardOutput()Acquires the standard output stream.
Read()Reads the next character from the standard input stream.
ReadKey()Obtains the next character or function key pressed by the user. The pressed key is displayed in the console window.
ReadLine()Reads the next line of characters from the standard input stream.
ResetColor()Sets the foreground and background console colors to their defaults.
SetBufferSize(Int32, Int32)Sets the height and width of the screen buffer area to the specified values.
SetCursorPosition(Int32, Int32)Sets the position of the cursor.
SetError(TextWriter)Sets the Error property to the specified TextWriter object.
SetIn(TextReader)Sets the In property to the specified TextReader object.
SetOut(TextWriter)Sets the Out property to the specified TextWriter object.
SetWindowPosition(Int32, Int32)Sets the position of the console window relative to the screen buffer.
SetWindowSize(Int32, Int32)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:




// C# program to illustrate the concept
// of WriteLine(String) method in console
using System;
  
public class GFG {
  
    static public void Main()
    {
  
        // WriteLine(String) method is used 
        // to display the string
        Console.WriteLine("Welcome to GeeksforGeeks");
        Console.WriteLine("This is a tutorial of Console Class");
    }
}
Output:
Welcome to GeeksforGeeks
This is a tutorial of Console Class

Events

EventDescription
CancelKeyPressOccurs 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).

Reference:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :