C# Console BufferWidth Property

The Console.BufferWidth property in C# gets or sets the width of the buffer area in columns. The buffer area is the region of memory that stores console output before it's displayed on the screen. This property is particularly useful when you need to control the console display width programmatically.

Syntax

Following is the syntax for getting the buffer width −

int width = Console.BufferWidth;

Following is the syntax for setting the buffer width −

Console.BufferWidth = value;

Return Value

The property returns an int representing the width of the buffer area in columns. On some platforms or environments where console buffer operations are not supported, it may return 0.

Getting Buffer Width

The following example demonstrates how to retrieve the current buffer width −

using System;

class Demo {
    static void Main() {
        Console.WriteLine("Buffer width (columns) = " + Console.BufferWidth);
        Console.WriteLine("Window width (columns) = " + Console.WindowWidth);
    }
}

The output of the above code is −

Buffer width (columns) = 120
Window width (columns) = 120

Setting Buffer Width

You can also modify the buffer width to control the console display. Here's an example that sets a custom buffer width −

using System;

class Demo {
    static void Main() {
        Console.WriteLine("Original buffer width: " + Console.BufferWidth);
        
        // Set buffer width to 80 columns
        Console.BufferWidth = 80;
        Console.WriteLine("New buffer width: " + Console.BufferWidth);
        
        // Display a long line to see the effect
        Console.WriteLine("This is a long line of text that will wrap based on the buffer width setting.");
    }
}

The output of the above code is −

Original buffer width: 120
New buffer width: 80
This is a long line of text that will wrap based on the buffer width setting.

Using Buffer Width with Text Formatting

The buffer width can be useful for creating formatted text displays −

using System;

class Demo {
    static void Main() {
        int width = Console.BufferWidth;
        if (width > 0) {
            // Create a line of dashes across the buffer width
            string separator = new string('-', width);
            Console.WriteLine("Buffer Width: " + width);
            Console.WriteLine(separator);
            Console.WriteLine("Text content here");
            Console.WriteLine(separator);
        } else {
            Console.WriteLine("Buffer width not available on this platform");
        }
    }
}

The output of the above code is −

Buffer Width: 120
------------------------------------------------------------------------------------------------------------------------
Text content here
------------------------------------------------------------------------------------------------------------------------

Conclusion

The Console.BufferWidth property provides control over the console buffer's width in columns. It's useful for formatting console output and creating responsive text-based interfaces that adapt to different console window sizes.

Updated on: 2026-03-17T07:04:35+05:30

278 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements