CodeGym /Courses /C# SELF /Keyboard Input

Keyboard Input

C# SELF
Level 2 , Lesson 5
Available

1. Introduction

Up to this point, we've been writing programs that were kinda like a radio: they told you something, but didn't care what you said back. Obviously, that's not gonna get us very far! The main goal of most programs is to do something useful for the user. So, a lot of the time, you need the user to be able to type stuff in from the keyboard.

For example:

  • A calculator expects you to enter numbers and an operation.
  • Website forms want your full name, email, and address to process your request.
  • Video games ask for the player's name.

Today, we're giving our app a microphone! Or, well, ears, if you prefer :) Ready? Let's go.

2. Getting to Know Console.ReadLine()

Console.ReadLine() is a function (or "command") that lets your program get data that the user types on the keyboard.

When your program hits the Console.ReadLine() command, it stops and... patiently waits until you type something and hit Enter. Everything you type before hitting Enter is treated as one line of text. That line gets sent back to your program—like the user handed you a note through a window.

Basic Example

// Let's ask the user for their name and greet them
Console.WriteLine("What's your name?");
string userName = Console.ReadLine(); 				// The program waits for input here!
    
Console.WriteLine("Hi, " + userName + "!");

How it works:

  • On the screen, you see: What's your name?
  • You type, for example, Andrey and hit Enter.
  • The variable userName gets the value "Andrey".
  • On the screen, you see: Hi, Andrey!

Heads up: whatever Console.ReadLine() returns is ALWAYS a string (type string). Even if the user typed a number, it's still a string!

3. Examples

Let's see how Console.ReadLine() is used in practice.

Example 1: entering the user's name

Console.WriteLine("Enter your name:");
string name = Console.ReadLine();
Console.WriteLine("Welcome, " + name + "!");

Example 2: outputting the result in one line

With the + operator, you can instantly combine text and the input result.

Console.Write("Your favorite color: ");
string color = Console.ReadLine();
Console.WriteLine("Cool! Your favorite color is " + color + ".");
Note:

in this example, Console.Write (without a new line) is used so the user's input goes right after the question.

Example 3: multiple inputs in a row

Let's make a mini-survey:

Console.Write("What's your name? ");
string name = Console.ReadLine();

Console.Write("How old are you? ");
string age = Console.ReadLine(); // still as a string

Console.WriteLine("Your name is " + name + ", and you are " + age + " years old.");

You'll see:

What's your name? Vika
How old are you? 23
Your name is Vika, and you are 23 years old.

4. Entering Numeric Data: Conversion

Here's a classic newbie trap! Console.ReadLine() always returns a string, even if the user typed 123.

If you want to get a number, you need to turn the string into the right numeric type (like int). In C#, you use conversion methods for this, like int.Parse() or Convert.ToInt32()—we already talked about this in the last lecture.

Example: asking for age

Console.Write("How old are you? ");
string input = Console.ReadLine();         // input = "27", for example
int age = int.Parse(input);                // Turn the string "27" into the number 27
Console.WriteLine("Next year you'll be " + (age + 1));
Attention:

if the user enters something that's not a number, the program will throw an error (FormatException). We'll learn how to write more reliable code later, but for simple experiments, this is enough.

5. Mistakes You're Definitely Gonna Run Into

Forgot to convert to a number:

Trying to use the value from Console.ReadLine() as a number without converting it.

string input = Console.ReadLine();
int sum = input + 5; // Error! You can't add a string to a number.

You need to convert to int:

int sum = int.Parse(input) + 5;

Forgot to declare the variable:

name = Console.ReadLine(); // Error! The variable name isn't declared.

You need to declare it first: string name = Console.ReadLine();

Mixed up the order of input and output:

Beginners often make silly mistakes. But pros do too. They just look smarter while doing it :)

string name = Console.ReadLine();
Console.WriteLine("Enter your name: " + name); // You should ask first, then read!

The right order:

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

Handy Note

Even if you use Console.ReadLine() without showing a question (like just string x = Console.ReadLine();), the program will WAIT for input. But the user won't know what they're supposed to type! So always give them a clear prompt using Console.Write() or Console.WriteLine().

2
Task
C# SELF, level 2, lesson 5
Locked
User Greeting
User Greeting
2
Task
C# SELF, level 2, lesson 5
Locked
Name-based Age Calculator
Name-based Age Calculator
1
Survey/quiz
Getting to Know int and string Types, level 2, lesson 5
Unavailable
Getting to Know int and string Types
Getting to Know int and string Types
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION