Loading...

C# Console Commands

Learn how to work with the C# console by writing output, reading user input, and formatting text with practical examples.

In console applications, input and output operations are performed with the System.Console class. This class provides many features, from writing text to the screen and reading data from the user, to changing the window title and text color. Knowing console commands well makes developing small applications much easier.


Console.Write / Console.WriteLine

To print output to the console, Write and WriteLine are used. Write continues on the same line, while WriteLine moves to the next line after printing.


Console.Write("Hello");
Console.Write(" World");   // Same line
Console.WriteLine("!");    // New line
// Output:
Hello World!

Console.ReadLine

To get input from the user, ReadLine is used. The entered value is always of type string.


Console.Write("Enter your name: ");
string name = Console.ReadLine();
Console.WriteLine("Hello " + name);

Console.ReadKey

To read a single key from the console, ReadKey is used. The pressed key’s character can be accessed with the KeyChar property.


Console.WriteLine("Press a key...");
var key = Console.ReadKey();
Console.WriteLine("\nYou pressed: " + key.KeyChar);

Formatted Output

When printing to the console, variables can be formatted using placeholders or string interpolation.


int age = 25;
string name = "Michael";

Console.WriteLine("{0} is {1} years old.", name, age);
Console.WriteLine($"{name} is {age} years old."); // Interpolation

Console.ForegroundColor & BackgroundColor

To change the text color or background color, use the ForegroundColor and BackgroundColor properties.


Console.ForegroundColor = ConsoleColor.Green;
Console.BackgroundColor = ConsoleColor.Black;
Console.WriteLine("Green text, black background.");
Console.ResetColor(); // Resets to default

Console.Clear & Console.Title

To clear the console, use Clear. To change the window title, use Title.


Console.Title = "My Console Application";
Console.Clear();

Console Window

The size and position of the console window can also be adjusted:


Console.WindowHeight = 30;
Console.WindowWidth = 100;
Console.WindowLeft = 0;
Console.WindowTop = 0;

Sample Application: Product Information

In this example, the user is asked for information about a table: color, height, width, length, and material. Based on the input, a formatted summary is displayed in the console.


Console.Title = "Product Information Entry";

Console.Write("Enter table color: ");
string color = Console.ReadLine();

Console.Write("Table height (cm): ");
int height = int.Parse(Console.ReadLine());

Console.Write("Table width (cm): ");
int width = int.Parse(Console.ReadLine());

Console.Write("Table length (cm): ");
int length = int.Parse(Console.ReadLine());

Console.Write("Is the table wooden (Y/N): ");
bool wooden = Console.ReadLine().Trim().ToUpper() == "Y";

// Colored output
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("\n--- Product Summary ---");
Console.ResetColor(); // Reset to default color

Console.WriteLine($"Color      : {color}");
Console.WriteLine($"Height     : {height} cm");
Console.WriteLine($"Width x Length : {width} x {length} cm");
Console.WriteLine($"Material   : {(wooden ? "Wood" : "Metal")}");