Lesson 8
50 XP

User Input

Getting User Input

Programs become interactive when they can accept input from users! In C#, we use Console.ReadLine() to read what the user types.

🎮 csharp
1
2
3
4
5
6
7
8
9
10
11
12
using System; class Program{    static void Main()    {        Console.WriteLine("What is your name?");        string name = Console.ReadLine();         Console.WriteLine("Hello, " + name + "!");    }}

How It Works:

  1. Program asks a question
  2. Waits for user to type and press Enter
  3. Stores the input in a variable
  4. Uses the input in the program

Example Interaction:

What is your name?
Alice
Hello, Alice!