Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Console Class in C#
The Console class in C# is part of the System namespace and provides methods and properties for interacting with the console window. It handles standard input, output, and error streams for console applications, allowing you to control cursor position, colors, buffer size, and other console characteristics.
Syntax
The Console class provides static properties and methods. Here are the basic syntax patterns −
Console.PropertyName = value; // Setting properties var value = Console.PropertyName; // Getting properties Console.MethodName(parameters); // Calling methods
Console Cursor Properties
Using Console.CursorLeft Property
The CursorLeft property gets or sets the column position of the cursor within the buffer area −
using System;
class Demo {
public static void Main(string[] args) {
Console.BackgroundColor = ConsoleColor.Blue;
Console.WriteLine("Background color changed = " + Console.BackgroundColor);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("\nForeground color changed = " + Console.ForegroundColor);
Console.CursorLeft = 30;
Console.Write("CursorLeft position: " + Console.CursorLeft);
}
}
The output will show the cursor repositioned to column 30 −
Background color changed = Blue
Foreground color changed = Yellow
CursorLeft position: 30
Using Console.CursorSize Property
The CursorSize property gets or sets the height of the cursor as a percentage (1 to 100) −
using System;
class Demo {
public static void Main(string[] args) {
Console.BackgroundColor = ConsoleColor.Blue;
Console.WriteLine("Background color changed = " + Console.BackgroundColor);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("\nForeground color changed = " + Console.ForegroundColor);
Console.CursorSize = 1;
Console.WriteLine("\nCursorSize = " + Console.CursorSize);
}
}
The output of the above code is −
Background color changed = Blue Foreground color changed = Yellow CursorSize = 1
Console Buffer Properties
Using Console.BufferWidth Property
The BufferWidth property gets or sets the width of the buffer area −
using System;
class Demo {
public static void Main(string[] args) {
Console.BufferHeight = 200;
Console.WriteLine("Buffer Height = " + Console.BufferHeight);
Console.BufferWidth = 250;
Console.WriteLine("Buffer Width = " + Console.BufferWidth);
}
}
The output of the above code is −
Buffer Height = 200 Buffer Width = 250
Common Console Properties
Common Console Properties Comparison
| Property | Description | Value Range |
|---|---|---|
| CursorLeft | Column position of cursor | 0 to BufferWidth-1 |
| CursorSize | Height of cursor as percentage | 1 to 100 |
| BackgroundColor | Background color of console | ConsoleColor enumeration |
| BufferWidth | Width of buffer area | Positive integer |
Conclusion
The Console class in C# provides comprehensive control over console applications through properties like cursor positioning, colors, and buffer dimensions. These properties enable developers to create more interactive and visually appealing console interfaces by manipulating the console's appearance and behavior programmatically.
