C# Console SetWindowSize() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Console

Definition

In C#, Console.SetWindowSize changes the visible console window’s width and height in character cells.

The window must fit inside the current screen buffer. If you need a larger window, increase the buffer first with SetBufferSize. Unsupported or redirected consoles may throw.

This is a static member of System.Console. You call it with the type name, such as Console.SetWindowSize(...). Console is the everyday API for reading from standard input and writing to standard output in C# console apps.

Syntax

Pass visible width and height; ensure the buffer is large enough.

C#
Console.SetBufferSize(100, 50);
Console.SetWindowSize(80, 25);
CallWhat it does
Console.SetWindowSize(width, height)Sets the visible window size.

Order matters: buffer first if you are growing the window.

Library declarations
C#
public static void SetWindowSize(int width, int height);

Parameters

ParameterDescription
widthVisible width in columns.
heightVisible height in rows.

Return Value

Return ValueDescription
voidWindow size changes when supported.

C# Examples

Example 1 — Shrink the Visible Window Safely

Ensure buffer is large enough, then set the window.

C#
using System;

class Program {
    static void Main() {
        try {
            int w = Math.Min(80, Console.BufferWidth);
            int h = Math.Min(25, Console.BufferHeight);
            Console.SetWindowSize(w, h);
            Console.WriteLine("Window: " + Console.WindowWidth + "x" + Console.WindowHeight);
        } catch (Exception ex) {
            Console.WriteLine("Not supported: " + ex.GetType().Name);
        }
    }
}

How It Works

  1. Clamp to buffer. Never request a window larger than the buffer.
  2. Try/catch. IDE hosts often disallow resize.
  3. Sample OK. Output shows a successful 80×25 window.
  4. Properties confirm. Read WindowWidth/Height afterward.

Example 2 — Grow Buffer Then Window

Increase buffer before asking for a wider window.

C#
using System;

class Program {
    static void Main() {
        try {
            int w = Math.Max(Console.BufferWidth, 100);
            int h = Math.Max(Console.BufferHeight, 40);
            Console.SetBufferSize(w, h);
            Console.SetWindowSize(100, 30);
            Console.WriteLine("OK");
        } catch (Exception) {
            Console.WriteLine("Resize not available");
        }
    }
}

How It Works

  1. Buffer first. Makes room for the larger window.
  2. Then window. SetWindowSize(100, 30) can succeed.
  3. Failure path. Prints a short message when unsupported.
  4. Interactive consoles. Desktop terminals are the usual target.

Example 3 — Show Current Window Size

Read sizes without changing them.

C#
using System;

class Program {
    static void Main() {
        Console.WriteLine("WindowWidth=" + Console.WindowWidth);
        Console.WriteLine("WindowHeight=" + Console.WindowHeight);
    }
}

How It Works

  1. No resize. Safe in every host.
  2. Sample values. Numbers vary by terminal.
  3. Planning. Read before calling SetWindowSize.
  4. Pairs with buffer. Compare to BufferWidth/Height.

Frequently Asked Questions

Window is what you see; buffer can be larger and scrollable.
Size larger than buffer, or host without a resizable console.
Yes when growing the window beyond the current buffer.
Often no. Wrap calls in try/catch for portability.
SetBufferSize and SetWindowPosition.

Next: Write()

Print text to the console without adding a new line at the end with System.Console.

Write() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful