Menu
Coddy logo textTech

Reading Input

Part of the Fundamentals section of Coddy's C# journey — lesson 33 of 69.

As of now we stored values that we thought about in variables. Programs usually don't work this way. We receive values from an outer source, a user for example.

In C#, getting input from a user is done using the Console.ReadLine() method. This method reads a line of text entered by the user in the console. The input is always returned as a string.

Here's an example of how to get input from a user:

string name = Console.ReadLine();
Console.WriteLine("Your name is: " + name);

In this example, the program waits for the user to enter their name and press Enter. The entered name is then stored in the name variable. Finally, the program prints a message that includes the entered name.

challenge icon

Challenge

Beginner

Write a program that gets input from the user (their name), and then outputs Hello,  followed by the user's inputted name.

For example, if the user inputs Bob, the expected output is Hello, Bob.

You will need to:

  1. Read the user's name using the appropriate Console method.
  2. Print Hello, and the stored variable in the end.

Cheat sheet

To get input from a user, use Console.ReadLine(). This method reads a line of text and returns it as a string:

string name = Console.ReadLine();
Console.WriteLine("Your name is: " + name);

The program waits for the user to enter text and press Enter before continuing.

Try it yourself

using System;

public class Program {
    public static void Main(string[] args) {
        // Read the user's name
        
        
        // Print the greeting message
        
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals