Skip to main content
C# Fundamentals for Beginners to Advanced
CHAPTER 06 Beginner

User Input and Output in C#

Updated: May 17, 2026
5 min read

# CHAPTER 6

User Input and Output

1. Introduction

A program that only talks to itself isn't very useful. Real applications interact with users. They ask for names, ages, and configurations. In C#, we handle this console interaction using Console.ReadLine() for input and Console.WriteLine() for output.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Use Console.ReadLine() to read text input from the user.
  • Convert (Parse) string input into numerical data types.
  • Handle formatting output using modern String Interpolation.
  • Build a functional User Registration form.

3. Reading Input with Console.ReadLine()

The Console.ReadLine() method waits for the user to type something and press ENTER. It always returns a string.
csharp
123456789101112131415
using System;

namespace InputDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter your username: "); // Write keeps cursor on same line
            string username = Console.ReadLine();
            
            Console.WriteLine("Welcome, " + username + "!");
        }
    }
}

4. Parsing Strings into Numbers

Because Console.ReadLine() only returns a string, what happens if you ask for an age?
csharp
1
// int age = Console.ReadLine(); // ERROR! Cannot convert string to int

You must Parse the string into an integer. You can do this using the Convert class or the type's built-in .Parse() method.

csharp
123456789
Console.Write("Enter your age: ");

// Method 1: Convert class
int age1 = Convert.ToInt32(Console.ReadLine()); 

// Method 2: Type parsing (Preferred by many developers)
int age2 = int.Parse(Console.ReadLine()); 

Console.WriteLine("You will be " + (age1 + 1) + " next year.");

5. Output Formatting (String Interpolation)

In the old days, combining text and variables looked like this: Console.WriteLine("Hello " + name + ", you are " + age + " years old."); This gets messy very quickly.

C# 6.0 introduced String Interpolation, which is the modern standard for formatting. You simply put a $ before the string, and you can place variables directly inside { } brackets.

csharp
12345
string name = "Alice";
int age = 28;

// Modern String Interpolation
Console.WriteLine($"Hello {name}, you are {age} years old.");

6. Mini Project: User Registration Form

Let's combine input, parsing, and interpolation into a small application.
csharp
123456789101112131415161718192021222324252627282930
using System;

namespace RegistrationForm
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("--- NEW USER REGISTRATION ---");
            
            Console.Write("First Name: ");
            string fName = Console.ReadLine();
            
            Console.Write("Last Name: ");
            string lName = Console.ReadLine();
            
            Console.Write("Age: ");
            int age = int.Parse(Console.ReadLine());
            
            Console.Write("Account Balance ($): ");
            decimal balance = decimal.Parse(Console.ReadLine());
            
            Console.WriteLine("\n--- REGISTRATION SUCCESSFUL ---");
            // Using String Interpolation
            Console.WriteLine($"User: {fName} {lName}");
            Console.WriteLine($"Age: {age}");
            Console.WriteLine($"Balance: ${balance}");
        }
    }
}

7. Common Mistakes

  • Crashing on Parse: If you use int.Parse() and the user types "Hello" instead of a number, the program will crash with a FormatException. (We will learn how to handle this safely using int.TryParse() later).
  • Forgetting the $ symbol: If you write "Hello {name}" without the $, it will literally print "Hello {name}" to the screen instead of replacing it with the variable's value.

8. Best Practices

  • Always use Console.Write() when asking for input so the user types their answer on the same line as the prompt.
  • Always use String Interpolation ($"") instead of the + operator when concatenating multiple variables into a string. It is faster and far easier to read.

9. Exercises

  1. 1. Write a program that asks for a temperature in Celsius, converts it to Fahrenheit (C * 9/5) + 32, and prints the result using string interpolation.
  1. 2. Write a program that asks for an item price and a quantity, calculates the total, and prints it out.

10. MCQs with Answers

Question 1

Which method is used to read a line of text typed by the user?

Question 2

What data type does Console.ReadLine() always return?

Question 3

If the user types "25", how do you safely store it in an integer variable?

Question 4

What is the modern way to combine strings and variables in C#?

Question 5

Which of the following is correct String Interpolation?

Question 6

What happens if you run int.Parse("Apple");?

Question 7

What is the difference between Console.Read() and Console.ReadLine()?

Question 8

Which class can also be used to convert strings to numbers?

Q9. True or False: You can place mathematical operations inside String Interpolation brackets, like {age + 5}. a) True b) False Answer: a) True
Question 10

Why use Console.Write() instead of Console.WriteLine() for input prompts?

11. Interview Questions

  • Q: Explain the difference between int.Parse() and Convert.ToInt32(). What happens if you pass null to both? (Answer: Parse throws an exception, Convert returns 0).
  • Q: What is String Interpolation, and why is it preferred over string concatenation?

12. Summary

Interactivity in C# console applications relies on Console.ReadLine(). Because all input is received as text (string), you must master parsing it into numerical types like int and decimal. When displaying data back to the user, String Interpolation ($"") provides a clean, modern way to format text.

13. Next Chapter Recommendation

Now that our programs can accept input, we need them to make decisions based on that input. In Chapter 7: Conditional Statements, we will learn how to use if, else, and switch.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·