Aral Code

Getting Started Lesson 3 of 15

C# Output and Input — WriteLine, ReadLine and String Interpolation

Print with Console.WriteLine and Write, build messages with string interpolation, format numbers, read input with Console.ReadLine, and convert it with int.Parse and TryParse.

Console programs talk to the user through text: printing results and reading what the user types. C# makes both easy, especially with string interpolation.

WriteLine and Write

Console.WriteLine("Line one");
Console.Write("Same ");
Console.Write("line");
Console.WriteLine();
Console.WriteLine(25 + 15);
Console.WriteLine("Total: " + 25 + 15);
Console.WriteLine("Total: " + (25 + 15));

Output

Line one
Same line
40
Total: 2515
Total: 40

WriteLine ends with a new line; Write doesn't. As in Java, + with text joins instead of adding, so wrap math in parentheses.

String interpolation

Put a $ before the string and write values inside { }. It's the cleanest way to build messages:

string name = "Maria";
int grade = 11;
double average = 92.456;

Console.WriteLine($"Name: {name}");
Console.WriteLine($"Grade {grade}, next year Grade {grade + 1}");
Console.WriteLine($"Average: {average:F2}");
Console.WriteLine($"Price: {1234567.5:N2}");
Console.WriteLine($"|{name,-10}|{grade,5}|");

Output

Name: Maria
Grade 11, next year Grade 12
Average: 92.46
Price: 1,234,567.50
|Maria     |   11|
FormatMeaningExample
{x:F2}Fixed, 2 decimals92.46
{x:N2}With thousands separators, 2 decimals1,234,567.50
{x:P0}Percent0.85 → 85%
{x,10} / {x,-10}Width 10, right / left alignedtable columns

Reading input with ReadLine

Console.Write("What's your name? ");
string name = Console.ReadLine();
Console.WriteLine($"Hello, {name}!");

Output

What's your name? Jose Rizal
Hello, Jose Rizal!

Console.ReadLine() waits for the user to type and press Enter, then returns the whole line as a string.

Converting input to numbers

Console.Write("Age: ");
int age = int.Parse(Console.ReadLine());

Console.Write("Price: ");
double price = double.Parse(Console.ReadLine());

Console.WriteLine($"Next year you'll be {age + 1}.");
Console.WriteLine($"Two of those cost {price * 2:F2}.");

Output

Age: 16
Price: 12.5
Next year you'll be 17.
Two of those cost 25.00.

TryParse: safe conversion

int.Parse("abc") crashes with a FormatException. int.TryParse returns true or false instead, and puts the number in an out variable:

Console.Write("How many pandesal? ");
string input = Console.ReadLine();

if (int.TryParse(input, out int qty))
{
    Console.WriteLine($"Total: {qty * 2.5:F2}");
}
else
{
    Console.WriteLine("Please type a whole number.");
}

Output

How many pandesal? sampu
Please type a whole number.

A grade calculator

Console.Write("Written works: ");
double ww = double.Parse(Console.ReadLine() ?? "0");
Console.Write("Performance tasks: ");
double pt = double.Parse(Console.ReadLine() ?? "0");
Console.Write("Quarterly exam: ");
double qe = double.Parse(Console.ReadLine() ?? "0");

double grade = ww * 0.25 + pt * 0.50 + qe * 0.25;
Console.WriteLine($"Quarterly grade: {grade:F2}");

Output

Written works: 88
Performance tasks: 92
Quarterly exam: 85
Quarterly grade: 89.25

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. What does $"{price:F2}" do?
2. What type does Console.ReadLine() return?
3. What does int.TryParse("abc", out int n) return?
4. What does Console.WriteLine("A" + 1 + 2); print?

By Sir Reyel · Last updated 2026-12-04