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.WindowWidth Property
The Console.WindowWidth property gets or sets the width of the console window measured in columns. This property is useful for creating console applications that need to adapt their output formatting based on the current window dimensions.
Syntax
Following is the syntax to get the console window width −
int width = Console.WindowWidth;
Following is the syntax to set the console window width −
Console.WindowWidth = value;
Return Value
The property returns an int representing the width of the console window in columns. When setting the property, the value must be within the valid range supported by the console buffer.
Getting Console Window Width
The following example demonstrates how to retrieve the current console window width −
using System;
class Demo {
static void Main() {
int width = Console.WindowWidth;
int height = Console.WindowHeight;
Console.WriteLine("Current window width = " + width);
Console.WriteLine("Current window height = " + height);
Console.WriteLine("Console buffer size = " + Console.BufferWidth + " x " + Console.BufferHeight);
}
}
The output of the above code is −
Current window width = 120 Current window height = 30 Console buffer size = 120 x 300
Setting Console Window Width
You can also modify the console window width programmatically. The following example shows how to resize the console window −
using System;
class Demo {
static void Main() {
Console.WriteLine("Original width: " + Console.WindowWidth);
// Set new window width (must be within valid range)
try {
Console.WindowWidth = 80;
Console.WriteLine("New width: " + Console.WindowWidth);
// Display a line that fits the new width
Console.WriteLine(new string('-', Console.WindowWidth - 1));
}
catch (Exception ex) {
Console.WriteLine("Error: " + ex.Message);
}
}
}
The output of the above code is −
Original width: 120 New width: 80 -------------------------------------------------------------------------------
Practical Application
The following example demonstrates how to use WindowWidth to create formatted output that adapts to the console size −
using System;
class Demo {
static void Main() {
int width = Console.WindowWidth;
string title = "Console Application";
// Center the title
int padding = (width - title.Length) / 2;
Console.WriteLine(new string(' ', padding) + title);
// Create a separator line
Console.WriteLine(new string('=', width - 1));
// Display information
Console.WriteLine("Window dimensions: " + Console.WindowWidth + " x " + Console.WindowHeight);
Console.WriteLine("This text adapts to your console width!");
}
}
The output of the above code is −
Console Application
===========================================================================
Window dimensions: 80 x 25
This text adapts to your console width!
Conclusion
The Console.WindowWidth property provides a way to get and set the console window width in columns. It's essential for creating responsive console applications that can adapt their formatting and layout based on the current window size, ensuring optimal display across different console configurations.
