Chapter 8: User Input and Output

1. Output – Showing Text to the User (Console.WriteLine & Friends)

You already know Console.WriteLine() – it’s the most common way to print text.

But there are several useful variations:

Method What it does Example Result on screen
Console.WriteLine() Print text + new line Console.WriteLine(“Hello”); Hello (cursor moves to next line)
Console.Write() Print text without new line Console.Write(“Hi”); Console.Write(“!”); Hi! (all on same line)
Console.WriteLine(“text”) With formatting Console.WriteLine(“Age: {0}”, 25); Age: 25
Console.WriteLine($”…”) String interpolation (modern & best) Console.WriteLine($”Hi {name}!”); Hi Webliance!

Real example – Beautiful output:

C#

2. Input – Getting Data from the User (Console.ReadLine)

The only way to read a full line of text from the console is:

C#
  • It waits until the user presses Enter
  • Returns everything the user typed as a string
  • Never returns null in modern .NET (it returns empty string “” if user just presses Enter)

Important: Console.ReadLine() always returns a string – even if the user types a number!

3. Parsing – Converting String to Numbers (Very Important!)

Because input is always string, you need to convert it to int, double, etc.

Here are the most common & safe ways:

Method Use when Example Throws exception if invalid?
int.Parse(string) You are 100% sure input is valid int age = int.Parse(“25”); Yes
Convert.ToInt32(string) Same as above int age = Convert.ToInt32(“25”); Yes
int.TryParse(string, out int) Safest – recommended in real apps if (int.TryParse(input, out int age)) { … } No – returns false
double.Parse / double.TryParse For decimal numbers double price = double.Parse(“99.99”); Yes / No
decimal.Parse / decimal.TryParse For money (exact) decimal salary = decimal.Parse(“75000.50”); Yes / No

Best practice in 2026: Always use TryParse when getting input from users – it prevents your program from crashing if the user types something wrong!

4. Real Examples – Safe Input with TryParse

Bad way (will crash if user types letters):

C#

Good & safe way (recommended):

C#

Even better – Keep asking until valid input:

C#

5. Other Useful Input Methods

  • Console.ReadKey() – Read one single key without pressing Enter
C#
  • Console.Read() – Read one character (rarely used)

Mini-Project: Smart Calculator (With Safe Input)

C#

Quick Cheat Sheet – Input & Output

C#

Your Homework (Super Practical!)

  1. Create a new console project called UserInputMaster
  2. Make a Personal Info Collector program that:
    • Asks for name (string)
    • Asks for age (int – safe with TryParse, keep asking until valid)
    • Asks for height in cm (double – safe)
    • Asks for favorite color (string)
    • Prints a beautiful summary card
  3. Bonus: Add yes/no confirmation at the end using Console.ReadKey()

Next lesson: Exception Handling – we’re going to make your programs bulletproof against crashes!

You’re doing absolutely fantastic! 🎉 Any part confusing? Want more examples with TryParse or formatting? Just tell me — I’m right here for you! 💙

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *