Skip to main content

The Console Class

Every program so far has run inside the Console window, mainly using Console.WriteLine() and Console.ReadLine().

That entire window is actually controllable from code! You can read and change its properties and call its other ready-made methods :^)

Learning on Your Own​

Finding answers yourself is one of the most important skills a developer has.

It comes down to three habits:

  1. Start with the official documentation 👉 It's the most reliable source. AI can hallucinate, so when they disagree with the docs, trust the docs.
  2. Compare several options 👉 When using Google, search with terms specific enough to describe your problem but general enough to find answers. When using AI, ask for 3 - 5 options and their trade-offs, not just one answer.
  3. Verify before you trust 👉 Test code yourself and check claims against the docs. Some articles are AI-generated regurgitations of other AI-generated content, so prefer sources with a named author, a recent date, and working examples.

What Is Console?​

Console is a static class in the System namespace. Like most classes, it has:

  • Properties: values that describe it (e.g. its background color).
  • Methods: actions it can perform (e.g. clearing the screen).

Full list: Console Class – Microsoft Learn. Use the table of contents on the right to jump to the Properties and Methods sections.

Properties​

A property is either read/write or read-only. Its definition in the docs tells you which.

Read/Write: { get; set; }​

Lets take the following example:

public static ConsoleColor BackgroundColor { get; set; }

get means you can read the value, and set means you can change it:

Console.BackgroundColor = ConsoleColor.DarkBlue; // set
ConsoleColor current = Console.BackgroundColor; // get

In the docs, lines like [UnsupportedOSPlatform("ios")] above a definition list the platforms where that member doesn't work.

Read-Only: { get; }​

public static bool CapsLock { get; } // Windows only

You can read it but not assign to it:

if (Console.CapsLock)
{
Console.WriteLine("Caps lock is on!");
}

Property Types​

Every property has a type, which decides what values it can hold.

  • CapsLock is a bool
  • BackgroundColor is a ConsoleColor

When the type isn't a built-in one like int or string, the docs link to its own page.

Useful Properties​

PropertyPurpose
ForegroundColor, BackgroundColorText and background colors
CursorVisibleShow or hide the cursor
WindowWidth, WindowHeightWindow size in columns and rows
KeyAvailableWhether a key has been pressed

Cursor Position​

Positions are measured from the top left corner, which is column 0, row 0.

  • CursorLeft: column, counted from the left.
  • CursorTop: row, counted from the top.

Valid columns run from 0 to Console.WindowWidth - 1, and valid rows from 0 to Console.WindowHeight - 1.

grid

Methods​

Methods are actions that belong to a class. Because Console is static, you call them on the class itself:

Console.Clear();

The docs show each method's signature, which lists the inputs it expects:

public static void SetCursorPosition(int left, int top);

For Example, you can write a character in the middle of the window:

int middleX = Console.WindowWidth / 2;
int middleY = Console.WindowHeight / 2;

Console.SetCursorPosition(middleX, middleY);
Console.Write("X");

Useful Methods​

MethodPurpose
Clear()Erase all text and move the cursor to (0, 0)
SetCursorPosition(left, top)Move the cursor
GetCursorPosition()Return the cursor's current (Left, Top)
ReadKey()Read a single key press (pairs with KeyAvailable)
Beep()Play a sound; Beep(frequency, duration) is Windows-only