Coding/Learn/csharp/console io
C# Foundational Course
01.Why C# & .NET Power Modern Software
02.Anatomy of a C# Application
03.Variables & The C# Type System
04.Console I/O: Reading & Writing
05.String Interpolation & Modern Formatting
06.Arithmetic, Math Class & Modulo
07.Conditional Branching: if, else if, and else
08.Logical Operators (&&, ||, !)
09.Switch Statements & Modern Switch Expressions
10.While & Do-While Loops
11.The Standard For Loop & Counting
12.Loop Control: break and continue
13.Methods: Modular, Reusable Code Blocks
14.Optional Parameters, Named Arguments & out/ref
15.Fixed-Size Arrays & Zero-Based Indexing
16.The Sacred Foreach Loop
17.Dynamic Collections: List<T> & Generics
18.Classes & Objects: The OOP Blueprint
19.Auto-Properties & Constructors
20.Capstone: RPG Character & Inventory Engine
Course Certificate0%

Complete all lessons to earn

LearnConsole I/O: Reading & Writing
4 / 20 0%
Lesson 04 of 20
5 mins read

Console I/O: Reading & Writing

PreviousNext Lesson
In Plain English

Interactive applications communicate with users. Console.WriteLine() prints a line and advances the cursor, Console.Write() prints without a newline, and Console.ReadLine() captures user keyboard text. To convert text into numbers, use Convert.ToInt32() or int.Parse().

The Mental Model

"Console.WriteLine() is like sending a text message and hitting send. Console.Write() is typing letters without hitting send. Console.ReadLine() is waiting for the user to type a reply and press Enter."

Deep Dive: How It Works

Console.WriteLine(text): Prints text with an automatic trailing newline.

Console.Write(text): Prints text without advancing to the next line (perfect for input prompts: Console.Write("Enter name: ");).

Console.ReadLine(): Reads a full line of text entered by the user as a string.

Type Parsing: Console.ReadLine() always returns a string. To convert to an int: int age = int.Parse(Console.ReadLine()); or int age = Convert.ToInt32(Console.ReadLine());.

Syntax Blueprint

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

Prompt with Write(), capture with ReadLine(), echo with WriteLine().

PointCore Rules to Remember

Point star
Write vs WriteLine: Write() keeps cursor on same line; WriteLine() adds a newline.
Point star
ReadLine Returns String: Always returns string data that must be parsed if numeric.
Point star
Safe Parsing: int.TryParse() is used in production to safely handle invalid numeric user input.

Common Beginner Traps & How to Fix Them

Mistake:Trying to assign ReadLine directly to an int: int age = Console.ReadLine();

Why it happens: Expecting automatic type casting.

How to fix: Wrap in int.Parse(): int age = int.Parse(Console.ReadLine());.

Pro Tip: Use int.TryParse(input, out int result) to convert user input without crashing if they type letters instead of numbers!

Live Interactive Example

Hit Run Code to see it live
Console Formatting & Parsing
C# 12 / .NET
1
2
3
4
5
6
7
8
9
10
11
12
13
Output Console
Click "Run Code" to view the rendered output.
How it works: The string "25" is parsed into an int and used in an arithmetic calculation.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Parse string to integer

  • PointDeclare string inputScore = "85";
  • PointParse it into int score = int.Parse(inputScore);
  • PointPrint "Final Score: 85".
1
2
3
4
5
6
7
8
9
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates