User Input and Output in C#
# 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 usingConsole.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.
4. Parsing Strings into Numbers
BecauseConsole.ReadLine() only returns a string, what happens if you ask for an age?
You must Parse the string into an integer. You can do this using the Convert class or the type's built-in .Parse() method.
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.
6. Mini Project: User Registration Form
Let's combine input, parsing, and interpolation into a small application.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 aFormatException. (We will learn how to handle this safely usingint.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.
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.
- 2. Write a program that asks for an item price and a quantity, calculates the total, and prints it out.
10. MCQs with Answers
Which method is used to read a line of text typed by the user?
What data type does Console.ReadLine() always return?
If the user types "25", how do you safely store it in an integer variable?
What is the modern way to combine strings and variables in C#?
Which of the following is correct String Interpolation?
What happens if you run int.Parse("Apple");?
What is the difference between Console.Read() and Console.ReadLine()?
Which class can also be used to convert strings to numbers?
{age + 5}.
a) True b) False
Answer: a) True
Why use Console.Write() instead of Console.WriteLine() for input prompts?
11. Interview Questions
-
Q: Explain the difference between
int.Parse()andConvert.ToInt32(). What happens if you passnullto both? (Answer:Parsethrows an exception,Convertreturns 0).
- Q: What is String Interpolation, and why is it preferred over string concatenation?
12. Summary
Interactivity in C# console applications relies onConsole.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 useif, else, and switch.