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.SetBufferSize() Method in C#
The Console.SetBufferSize() method in C# sets the height and width of the console screen buffer area to the specified values. The buffer size determines how much text the console can hold in memory, which affects scrolling and display capabilities.
Syntax
Following is the syntax for the Console.SetBufferSize() method −
public static void SetBufferSize(int width, int height);
Parameters
-
width − The width of the buffer area measured in columns.
-
height − The height of the buffer area measured in rows.
The buffer size must be greater than or equal to the window size. If you set a buffer size smaller than the current window size, an ArgumentOutOfRangeException will be thrown.
Example
Let us see an example to implement the Console.SetBufferSize() method −
using System;
class Demo {
public static void Main(string[] args) {
Console.WriteLine("Original buffer size: " + Console.BufferWidth + "x" + Console.BufferHeight);
Console.WriteLine("Original window size: " + Console.WindowWidth + "x" + Console.WindowHeight);
Console.SetBufferSize(120, 50);
Console.BackgroundColor = ConsoleColor.DarkBlue;
Console.ForegroundColor = ConsoleColor.White;
Console.Clear();
Console.WriteLine("New buffer size: " + Console.BufferWidth + "x" + Console.BufferHeight);
Console.WriteLine("Window size: " + Console.WindowWidth + "x" + Console.WindowHeight);
Console.WriteLine();
for (int i = 1; i
The output of the above code will show the buffer and window dimensions, followed by multiple lines that demonstrate the scrollable buffer −
Original buffer size: 80x25
Original window size: 80x25
New buffer size: 120x50
Window size: 80x25
Line 1: This is a demonstration of the larger buffer size.
Line 2: This is a demonstration of the larger buffer size.
...
Line 40: This is a demonstration of the larger buffer size.
Buffer is larger than window - you can scroll to see all content!
Using SetBufferSize with Error Handling
Here is an example that demonstrates proper error handling when setting buffer size −
using System;
class BufferSizeExample {
public static void Main(string[] args) {
try {
Console.WriteLine("Current window size: " + Console.WindowWidth + "x" + Console.WindowHeight);
// This will work - buffer larger than window
Console.SetBufferSize(150, 60);
Console.WriteLine("Successfully set buffer to 150x60");
// Reset to demonstrate error
Console.SetBufferSize(80, 25);
// This would throw exception - buffer smaller than window
// Console.SetBufferSize(40, 10); // Uncomment to see error
}
catch (ArgumentOutOfRangeException ex) {
Console.WriteLine("Error: " + ex.Message);
}
catch (Exception ex) {
Console.WriteLine("Unexpected error: " + ex.Message);
}
}
}
The output of the above code is −
Current window size: 80x25
Successfully set buffer to 150x60
Key Points
-
The buffer size must be greater than or equal to the window size.
-
A larger buffer allows for more scrollable content in the console.
-
Use
Console.BufferWidthandConsole.BufferHeightto get current buffer dimensions. -
This method is primarily useful in Windows console applications.
Conclusion
The Console.SetBufferSize() method allows you to control the console buffer dimensions, enabling more content to be stored and scrolled through. Always ensure the buffer size is at least as large as the window size to avoid runtime exceptions.
