Lesson 1.7 – Accepting Input in Command Line Applications
C# Course for Absolute Beginners – Lesson 1.7
Next Lesson
Introduction
In this lab you’ll learn how to accept input into the console application. For clarification – "Input" means allowing the user to type something and to send values into our application.
You’ve already seen how to use the ReadLine method to stop execution of the console app in order to review what was on screen before the console window closes. When the user selects the return key or the enter key on their keyboard, the application continues on. This same method actually returns a value. So some methods can accept parameters, some methods can return values, some do both, and some do neither.
Step 1: Capturing User Data and Displaying the Data to the User

First, either start a new project or delete the code from the previous project. We’ll use the Console Window to create a simple user interface, and we’ll use WriteLine to tell our user what we expect from them. Enter the following code:
Console.WriteLine("Please type something and press Return." );
Second, we’ll create a variable to hold a string value called "userValue." Type the following line of code:
string userValue;
Third, we want to save whatever the user types. We’re going to take whatever the user enters, and use ReadLine to save it into the new string variable. Type the following line of code:
userValue = Console.ReadLine();
Fourth, we’ll display whatever the user entered. We’ll add "You typed:" at the beginning, display the user value at the end, and combine them with the addition sign (+). Type the following line of code:
Console.WriteLine("You typed: " + userValue);
Finally, we’ll add the obligatory ReadLine at the very end, so that we can actually see what the application outputs. Add the following line of code:
Console.ReadLine();
NOTE:
In Day 02 we’ll discuss operators like the addition sign. We used the addition sign in lesson 1.5, if you recall. Here, we can also use the addition sign as a string concatenator, to combine two strings together. This is not the preferred method – there’s a better way, but for simple examples this will work.
Step 2: Testing the Application

Select the Start Debugging button in the toolbar. You’ll see the Console Window appear, displaying our original message. For our example we’ll enter 42.

When we select the Return key on our keyboard the Console Window displays "You typed 42." So it’s as simple as that.
Step 3: Understanding the Console.ReadLine(); Method

The important concept here is that the ReadLine method we created actually returns a value.
(1) In line 16 we decided to capture the value of the first ReadLine method.
(2) In line 18 we decided to ignore the value of the second ReadLine method.
So – after the first response, a user could type in more information, or even random characters, and then hit return on the keyboard. The Console Window would then close. You may be wondering, "What happened to all the text the user typed in?" It’s thrown away, .NET gets rid of it. What’s important to understand is that this is how we accept input into a console application.
At the very end of the Day 01 set of labs you’ll be asked to require user input, so it’s important that you understand this process before you get to that point. As you can see it’s very simple to read information that the user has typed in to a console window.
Conclusion
In this brief lab you learned that the ReadLine method captures data. You learned how to create a variable to store that data, and then display it back to the user. Finally, you learned that the data is discarded once the program terminates.

