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
Getting the Largest Window Height and Width of the Console in C#
The Console class in C# provides properties to determine the maximum possible window dimensions for a console application. The LargestWindowHeight and LargestWindowWidth properties return the largest height and width that the console window can have on the current system.
These properties are useful when you need to maximize the console window or ensure your application's display fits within the system's constraints.
Syntax
Following is the syntax for getting the largest window height −
int maxHeight = Console.LargestWindowHeight;
Following is the syntax for getting the largest window width −
int maxWidth = Console.LargestWindowWidth;
Getting the Largest Window Height
The Console.LargestWindowHeight property returns the maximum number of console window rows that can be displayed on the current system −
using System;
public class Demo {
public static void Main(string[] args) {
Console.WriteLine("Largest Window Height of the Console = " + Console.LargestWindowHeight);
Console.WriteLine("Current Window Height = " + Console.WindowHeight);
}
}
The output of the above code is −
Largest Window Height of the Console = 58 Current Window Height = 30
Getting the Largest Window Width
The Console.LargestWindowWidth property returns the maximum number of console window columns that can be displayed on the current system −
using System;
public class Demo {
public static void Main(string[] args) {
Console.WriteLine("Largest Window Width of the Console = " + Console.LargestWindowWidth);
Console.WriteLine("Current Window Width = " + Console.WindowWidth);
}
}
The output of the above code is −
Largest Window Width of the Console = 190 Current Window Width = 120
Using Both Properties Together
You can retrieve both the maximum height and width simultaneously to understand the complete console window constraints −
using System;
public class Demo {
public static void Main(string[] args) {
int maxHeight = Console.LargestWindowHeight;
int maxWidth = Console.LargestWindowWidth;
Console.WriteLine("Maximum Console Dimensions:");
Console.WriteLine("Height: " + maxHeight + " rows");
Console.WriteLine("Width: " + maxWidth + " columns");
Console.WriteLine("Total Characters: " + (maxHeight * maxWidth));
Console.WriteLine("\nCurrent Console Dimensions:");
Console.WriteLine("Height: " + Console.WindowHeight + " rows");
Console.WriteLine("Width: " + Console.WindowWidth + " columns");
}
}
The output of the above code is −
Maximum Console Dimensions: Height: 58 rows Width: 190 columns Total Characters: 11020 Current Console Dimensions: Height: 30 rows Width: 120 columns
Key Points
-
These properties are read-only and cannot be modified.
-
The values depend on the screen resolution and system settings.
-
They represent the theoretical maximum, not necessarily the practical usable space.
-
The actual console window size is controlled by
Console.WindowHeightandConsole.WindowWidth.
Conclusion
The Console.LargestWindowHeight and Console.LargestWindowWidth properties provide the maximum possible console window dimensions for the current system. These properties are essential for applications that need to optimize their display layout or maximize the console window programmatically.
