Console Input and Output

Beginner 20 min read Lesson 2 of 4

Console Output Methods

WriteLine vs Write

  • Console.WriteLine() - Writes text and moves to next line
  • Console.Write() - Writes text without moving to next line
Console.WriteLine("This is a line");
Console.Write("This is ");
Console.Write("the same line");
// Output: 
// This is a line
// This is the same line

String Formatting

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

// Method 1: Concatenation
Console.WriteLine("Hello " + name + ", you are " + age + " years old.");

// Method 2: String interpolation (C# 6+)
Console.WriteLine($"Hello {name}, you are {age} years old.");

// Method 3: Composite formatting
Console.WriteLine("Hello {0}, you are {1} years old.", name, age);

Console Input Methods

ReadLine()

Reads a line of text from the console:

Console.Write("Enter your name: ");
string name = Console.ReadLine();
Console.WriteLine($"Hello, {name}!");

ReadKey()

Reads a single character without requiring Enter:

Console.WriteLine("Press any key to continue...");
Console.ReadKey();
Console.WriteLine("Key pressed!");

Reading Numbers

Console.Write("Enter your age: ");
int age = Convert.ToInt32(Console.ReadLine());
// or
int age = int.Parse(Console.ReadLine());
// or with error handling
int age;
if (int.TryParse(Console.ReadLine(), out age))
{
    Console.WriteLine($"You are {age} years old.");
}
else
{
    Console.WriteLine("Invalid input!");
}
Best Practice

Always use TryParse when reading user input to handle invalid data gracefully.

Formatting Output

Numeric Formatting

double price = 19.99;
Console.WriteLine($"Price: {price:C}");        // Currency: $19.99
Console.WriteLine($"Price: {price:F2}");       // Fixed point: 19.99
Console.WriteLine($"Percentage: {0.75:P0}");   // Percentage: 75%
Console.WriteLine($"Number: {12345:N0}");       // Number with commas: 12,345

Alignment

Console.WriteLine($"{"Name",-20} {"Age",5}");
Console.WriteLine($"{"John",-20} {25,5}");
Console.WriteLine($"{"Jane",-20} {30,5}");
// Output:
// Name                 Age
// John                  25
// Jane                  30

Colors

Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("This is green text!");
Console.ResetColor();

Console.BackgroundColor = ConsoleColor.DarkBlue;
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("White text on blue background");
Console.ResetColor();
Key Takeaway

Mastering console input/output is essential for building interactive command-line applications. Practice with different formatting options.

Complete Example

using System;

class Program
{
    static void Main()
    {
        Console.Title = "User Information";
        Console.ForegroundColor = ConsoleColor.Cyan;
        Console.WriteLine("=== USER INFORMATION ===\n");
        Console.ResetColor();

        // Get user input
        Console.Write("Enter your name: ");
        string name = Console.ReadLine();

        Console.Write("Enter your age: ");
        int age;
        while (!int.TryParse(Console.ReadLine(), out age) || age < 0)
        {
            Console.Write("Please enter a valid age: ");
        }

        Console.Write("Enter your salary: ");
        double salary;
        while (!double.TryParse(Console.ReadLine(), out salary) || salary < 0)
        {
            Console.Write("Please enter a valid salary: ");
        }

        // Display formatted output
        Console.Clear();
        Console.WriteLine("=== USER INFORMATION ===\n");
        Console.WriteLine($"{"Name:",-15} {name}");
        Console.WriteLine($"{"Age:",-15} {age} years");
        Console.WriteLine($"{"Salary:",-15} {salary:C}");
        Console.WriteLine($"{"Status:",-15} {(age >= 18 ? "Adult" : "Minor")}");

        Console.WriteLine("\nPress any key to exit...");
        Console.ReadKey();
    }
}
Exercise

Create a console application that:

  1. Asks for first name, last name, and age
  2. Displays the information in a formatted table
  3. Uses colors to highlight important information
  4. Handles invalid input gracefully
Test Your Knowledge - Take Quiz