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
C# Console.WindowHeight Property
The Console.WindowHeight property in C# gets or sets the height of the console window measured in rows. This property allows you to retrieve the current console window height or modify it programmatically to control the display area of your console application.
Syntax
Following is the syntax for getting the window height −
int height = Console.WindowHeight;
Following is the syntax for setting the window height −
Console.WindowHeight = newHeight;
Return Value
The property returns an int value representing the height of the console window in rows. When setting, it accepts an int value that specifies the new height.
Getting Console Window Height
You can retrieve the current console window height and display it −
using System;
class Demo {
static void Main() {
int height;
height = Console.WindowHeight;
Console.WriteLine("Current window height = " + height);
Console.WriteLine("Window can display " + height + " rows of text");
}
}
The output of the above code is −
Current window height = 30 Window can display 30 rows of text
Setting Console Window Height
You can modify the console window height to customize the display area −
using System;
class Demo {
static void Main() {
Console.WriteLine("Original height: " + Console.WindowHeight);
try {
Console.WindowHeight = 20;
Console.WriteLine("New height set to: " + Console.WindowHeight);
}
catch (Exception ex) {
Console.WriteLine("Error setting height: " + ex.Message);
}
}
}
The output of the above code is −
Original height: 30 New height set to: 20
Using WindowHeight with Buffer Operations
The window height works together with buffer height to control scrolling behavior −
using System;
class Demo {
static void Main() {
Console.WriteLine("Window Height: " + Console.WindowHeight);
Console.WriteLine("Buffer Height: " + Console.BufferHeight);
Console.WriteLine("Window Width: " + Console.WindowWidth);
Console.WriteLine("\nWindow dimensions: " +
Console.WindowWidth + " x " + Console.WindowHeight);
}
}
The output of the above code is −
Window Height: 30 Buffer Height: 300 Window Width: 120 Window dimensions: 120 x 30
Key Rules
-
The window height cannot exceed the buffer height (
Console.BufferHeight). -
Setting an invalid height value may throw an
ArgumentOutOfRangeException. -
The actual behavior may vary depending on the operating system and console host.
-
In some environments (like online compilers), console window properties may return default values.
Conclusion
The Console.WindowHeight property provides control over the console window's vertical size in rows. It's useful for creating console applications that need to manage their display area, though behavior may vary across different platforms and console hosts.
