In this article, we are going to learn how to solve simple problems by using linear structures. We are going to accept inputs, work with them and print the output to the console window.

For the complete navigation of this series check out: C# Back to Basics.

If you want to download the source code for our examples, you can do that from here Input and Output Source Code.

So let’s start.

Example 1: Create an application that prints out the sum of two integer values which user inputs in the console window.

Let’s create a new console application and name it SumGenerator. Then let’s type this code inside the Main method:

With the Console.WriteLine() statement, we display the message on the console window and move to the next line. The Console.ReadLine() statement will read our input, but it is of type string and what we need is an int type. So, we need to convert it with the Convert.ToInt32() statement. Finally, we calculate the sum and print it out. The Console.ReadKey() statement is here just to keep our console window open.

Let’s press F5 to start our application and enter two integer numbers:

Sum generator Input and Output in C#

Example 2: Write an application which for two provided inputs (name and last name), prints out the full name in a format: name <space> last name.

Let’s create a new console application and to write this code:

That’s it. If we run our project by hitting F5, we will see the result with the name and last name, space separated.



Now we know how to manipulate inputs in our applications and how to display the result in the console window. Furthermore, we have used the data conversion from string to int, to be able to sum the inputs from a user.

If you have enjoyed reading this article and if you would like to receive the notifications about the freshly published .NET Core content we encourage you to subscribe to our blog.