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

To download a source code, you can visit our Input and Output Source Code repository.

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

We are going to divide this article into the following sections:

So let’s start.

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

Support Code Maze on Patreon to get rid of ads and get the best discounts on our products!
Become a patron at Patreon!

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

namespace SumGenerator
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Write the first integer:");
            int first = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Write the second integer:");
            int second = Convert.ToInt32(Console.ReadLine());

            int result = first + second;
            Console.WriteLine($"The result is {result}");

            Console.ReadKey();
        }
    }
}

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:

Write the first integer:
5
Write the second integer:
The result is 33

Using Inputs (first name and last name) and Printing Out the Full Name with Linear Structures in C#

Example 2: Write an application that 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 write the code:

namespace FullNameGenerator
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("What is your first name:");
            string name = Console.ReadLine();

            Console.WriteLine("What is your last name:");
            string lastName = Console.ReadLine();

            string fullName = name + " " + lastName;
            Console.WriteLine($"Your full name is: {fullName}");

            Console.ReadKey();
        }
    }
}

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

Conclusion

Excellent.

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.

In the next post, we are going to show you how to work with strings in C#.

Liked it? Take a second to support Code Maze on Patreon and get the ad free reading experience!
Become a patron at Patreon!