How to read user inputs from a console

A beginner guide to programming with .NET 5 and C#

In this article, we explore how to read user inputs from the console. This article is the foundation of more dynamic notions enabling our programs to change based on user interactions and react to them. We also learn how to change the title of the console and how to delete its content.

This article is part of a learn programming series where you need no prior knowledge of programming. If you want to learn how to program and want to learn it using .NET/C#, this is the right place. I suggest reading the whole series in order, starting with Creating your first .NET/C# program, but that’s not mandatory.

User input

There are multiple types of applications, like web apps, mobile apps, and Windows apps. In our case, we will continue to use console applications because they are the simplest. We don’t need to bother with complex user interfaces, animation, or interaction, and we can focus on learning to program.

Console apps may feel less exciting, but that allows us to focus on only one subject at a time. Remember that every piece of knowledge that you are acquiring is like a new LEGO ®block that you’ll be able to piece with the others later. Moreover, what you are learning in this series is reusable in most, if not all, other types of apps.

The Console class offers three methods to read user inputs, Read, ReadKey, and ReadLine. We can use the first two for more complex scenarios. The third one is very straightforward and is the method we are focusing on in this article.

As its name implies, Console.ReadLine reads the line entered by the user. It is simple and gives us the power to accomplish what we need, to learn the basic programming concepts.

Reading a line entered by a user

using System;Console.Write("Please enter a greeting message, then press ENTER: ");
var hello = Console.ReadLine();
Console.WriteLine(hello);

In the preceding code, we write a message to the user, then wait for an input. The program will block its execution there until the user hits the <ENTER> key. At this point, it will resume and continue, then write the read line back to the console.

Here is the console content when running the program and entering Hello Amigo!<ENTER>:

Please enter a greeting message, then press ENTER: Hello Amigo!
Hello Amigo!

More info: it is important to note that the new line character (the <ENTER>) is not part of the line (not saved in the hello variable).

Now that we saw an example, let’s explore the flow of execution of the program.

Flow of execution

Here is what happens:

  1. The program writes the question to the console.
  2. The program executes the right-end of the assignation operator (=), the Console.ReadLine() method, which waits for the user to hit the <ENTER> key.
    More info: The assignation operator = is always the last to be executed; it has the lowest priority.
  3. The user types Hello Amigo! then hit <ENTER>.
  4. The program then resumes and assigns the user-entered value to the hello variable.
  5. The program writes that input back to the console.

Here is a second way to visualize this flow:

In this case, the Console.ReadLine() method manages the bifurcation even if code-wise, the flow is linear.

Note: We will learn ways to control a program’s flow in future articles.

Next, it is your turn to try this out.

Exercise

Then, the program must greet that user using the following format: Greetings {first name} {last name}!.

Example: Assuming the user entered Carl-Hugo as the first name and Marcotte as the last name, the greeting message would read Greetings Carl-Hugo Marcotte!.

Unfortunately, I was not able to recreate the whole exercise on this platform, so please look at the exercise on the original post on my blog. I’m sorry for the inconvenience.

Bonus information

  • How to set its title.
  • How to clear what is written in it.

The block of code at the end of this section contains the preceding exercise’s solution; please be advised.

Setting a custom console title

Console.Title = "IntroToDotNet";

Pretty straightforward, isn’t it? Next, let’s see how to clear the text from the console.

Clearing the console

What is your first name? Carl-Hugo
What is your last name? Marcotte
Greetings Carl-Hugo Marcotte!

We could clear the console between each question to obtain the following flow:

Here is the code to achieve that result:

using System;Console.Title = "IntroToDotNet"; // Custom titleConsole.Write("What is your first name? ");
var firstName = Console.ReadLine();
Console.Clear(); // Clear after the first question
Console.Write("What is your last name? ");
var lastName = Console.ReadLine();
Console.Clear(); // Clear after the second question
Console.Write("Greetings ");
Console.Write(firstName);
Console.Write(" ");
Console.Write(lastName);
Console.WriteLine("!");

And that’s it for this article.

Conclusion

We also looked at how to change the title of the terminal Window because why not, right? Finally, we explored how to clear the text to reset the console to an empty state. This second interlude can be very handy at crafting a better user experience (UX).

Next step

Originally published at https://www.forevolve.com on March 14, 2021.

Carl has been developing, designing, and architecting web applications professionally since 2005. He is passionate about software architecture, C#, and .NET

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store