In this lesson, you'll put your newfound C# skills to the test by building a simple console application. This application will take input from the user, perform a calculation based on that input, and then display the result back to the user. This hands-on experience will solidify your understanding of variables, data types, operators, and control flow, all while giving you a tangible project to showcase your progress. This task directly applies the concepts you've learned in the previous lessons, reinforcing your understanding and preparing you for more complex projects in the future.

Building the Console Application

The goal is to create a console application that asks the user for two numbers, adds them together, and displays the sum. Here's a step-by-step guide:

  1. Project Setup: If you don't already have a project open from the previous lessons, create a new console application project in Visual Studio or VS Code. Name it something descriptive, like "CalculatorApp".
  2. Gathering User Input: The first step is to prompt the user to enter the two numbers. You'll use Console.WriteLine() to display the prompts and Console.ReadLine() to read the user's input. Since Console.ReadLine() returns a string, you'll need to convert the input to a numerical data type (like int or double) using int.Parse() or double.Parse().
  3. Performing the Calculation: Once you have the two numbers, you'll add them together using the + operator. Store the result in a variable.
  4. Displaying the Output: Finally, you'll use Console.WriteLine() to display the result to the user. You can use string concatenation or string interpolation to create a user-friendly message.

Code Example

Here's the C# code that implements the calculator application:

using System;

namespace CalculatorApp
{
    class Program
    {
        static void Main(string[] args)
        {
            // Prompt the user for the first number
            Console.Write("Enter the first number: ");
            string? input1 = Console.ReadLine(); // Read the first number as a string

            // Check if the input is null before parsing
            if (string.IsNullOrEmpty(input1))
            {
                Console.WriteLine("Invalid input. Please enter a number.");
                return; // Exit the program if the input is invalid
            }

            // Convert the first number to a double
            double num1;
            if (!double.TryParse(input1, out num1))
            {
                Console.WriteLine("Invalid input. Please enter a valid number.");
                return; // Exit the program if the input is invalid
            }

            // Prompt the user for the second number
            Console.Write("Enter the second number: ");
            string? input2 = Console.ReadLine(); // Read the second number as a string

            // Check if the input is null before parsing
            if (string.IsNullOrEmpty(input2))
            {
                Console.WriteLine("Invalid input. Please enter a number.");
                return; // Exit the program if the input is invalid
            }

            // Convert the second number to a double
            double num2;
            if (!double.TryParse(input2, out num2))
            {
                Console.WriteLine("Invalid input. Please enter a valid number.");
                return; // Exit the program if the input is invalid
            }

            // Calculate the sum
            double sum = num1 + num2;

            // Display the result using string interpolation
            Console.WriteLine($"The sum of {num1} and {num2} is: {sum}");

            // Keep the console window open until a key is pressed
            Console.ReadKey();
        }
    }
}

Code Explanation