C# VS2026
Lesson 04 of 30

Input and Output

C# in Visual Studio 2026 — a hands-on guide for developers at every level.

Writing Output

The Console class provides three methods for writing to the terminal:

Console.Write("No newline at the end");
Console.WriteLine("With a newline");
Console.WriteLine();  // blank line

String Interpolation

Prefix a string with $ to embed expressions directly inside curly braces — much cleaner than concatenation:

string name = "Bob";
int age = 30;

Console.WriteLine($"My name is {name} and I am {age} years old.");
Console.WriteLine($"Next year I will be {age + 1}.");

Format Specifiers

double price = 1234.5678;
Console.WriteLine($"{price:C}");    // $1,234.57  (currency)
Console.WriteLine($"{price:F2}");   // 1234.57   (2 decimal places)
Console.WriteLine($"{price:N0}");   // 1,235     (number, 0 decimals)
Console.WriteLine($"{price:E2}");   // 1.23E+003 (scientific)

Reading Input

Console.ReadLine() returns a string? (nullable string). Convert it to numbers with int.Parse or the safer int.TryParse:

Console.Write("Enter your name: ");
string name = Console.ReadLine() ?? "Unknown";

Console.Write("Enter your age: ");
if (int.TryParse(Console.ReadLine(), out int age))
    Console.WriteLine($"Hello, {name}! You are {age} years old.");
else
    Console.WriteLine("Invalid age entered.");
⚠️ Warning
int.Parse throws an exception if the input is not a valid number. Prefer int.TryParse when reading user input.