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 BufferHeight Property
The Console.BufferHeight property in C# gets or sets the height of the buffer area in rows. The buffer height represents the maximum number of text rows that can be stored in the console's internal buffer before older lines are discarded.
This property is particularly useful when you need to control how much output history the console retains or when working with console applications that produce large amounts of output.
Syntax
Following is the syntax for using the BufferHeight property −
// Get buffer height int height = Console.BufferHeight; // Set buffer height Console.BufferHeight = value;
Parameters
When setting the BufferHeight property:
value − An integer representing the number of rows for the buffer height. Must be greater than or equal to the current window height.
Return Value
Returns an int value representing the current buffer height in rows.
Getting Console Buffer Height
Example
using System;
class Demo {
static void Main() {
Console.WriteLine("Current buffer height: " + Console.BufferHeight + " rows");
Console.WriteLine("Current window height: " + Console.WindowHeight + " rows");
Console.WriteLine("Current buffer width: " + Console.BufferWidth + " columns");
}
}
The output of the above code is −
Current buffer height: 300 rows Current window height: 25 rows Current buffer width: 80 columns
Setting Console Buffer Height
Example
using System;
class Demo {
static void Main() {
Console.WriteLine("Original buffer height: " + Console.BufferHeight);
// Set buffer height to 500 rows
Console.BufferHeight = 500;
Console.WriteLine("New buffer height: " + Console.BufferHeight);
// Generate some output to demonstrate buffer
for (int i = 1; i
The output of the above code is −
Original buffer height: 300
New buffer height: 500
Line 1 - Buffer can now store more lines
Line 2 - Buffer can now store more lines
Line 3 - Buffer can now store more lines
Line 4 - Buffer can now store more lines
Line 5 - Buffer can now store more lines
Line 6 - Buffer can now store more lines
Line 7 - Buffer can now store more lines
Line 8 - Buffer can now store more lines
Line 9 - Buffer can now store more lines
Line 10 - Buffer can now store more lines
Buffer maintains history of all these lines
Key Rules
The buffer height must be greater than or equal to the window height.
Setting a smaller buffer height may cause loss of existing buffered content.
The property may not be supported on all platforms (returns 0 on some systems).
Changes to buffer height affect how much scrollback history is available.
Conclusion
The Console.BufferHeight property controls how many rows of text the console buffer can store. This is essential for applications that need to manage console output history and scrollback capabilities effectively.
