In this article, I have mentioned what is console application in C# with an example in Visual Studio.
The Console application is an application that runs in a console output window like a C and C++ program. The Console application does not have a graphical user interface. A Console Application takes inputs and displays output at a command-line console with access to three basic streams: standard input, standard output, and standard error.
Create Console Application in Visual Studio 2022
Before we create a console application in Visual Studio, we will understand these 3 methods to read user input and show output on the console:
Console.ReadLine()
– This method is used to read the text as a string from console windows. This will read input text from the user at console windows and display string at console windows when the user presses the enter key.
Console.Write()
– write the text at console windows in the same line.
Console.WriteLine()
– Write the text at console windows and add a new line character at the end of the line.
Step 1: Open Visual Studio 2022 or 2019 and click on "Create new project"
Step 2: Search for the "Console application" and select the first one, as shown below in the image and click "Next"
Step 3: Once you have clicked "Next", you need to enter project name and then click on "Next"
Step 4: Now once you click "Next", you need to select .NET Core version, I have selected .NET 6
then simply Click "Create" and let Visual Studio generate the console application template.
Once we have a template, now navigate to the Program.cs
and use below code to create a program for addition or subtraction
using System;
class Program
{
static void Main(string[] args)
{
int num1 = 0;
int num2 = 0;
// Ask the user to type the first number.
Console.WriteLine("Enter a number");
num1 = Convert.ToInt32(Console.ReadLine());
// Ask the user to type the second number.
Console.WriteLine("Enter Second number");
num2 = Convert.ToInt32(Console.ReadLine());
// Ask the user to choose an option.
Console.WriteLine("Select Operation:");
Console.WriteLine("1 - Add");
Console.WriteLine("2 - Subtract");
// Use a switch statement to do the math.
switch (Console.ReadLine())
{
case "1":
Console.WriteLine("Your result: " + num1+ " + "+num2 +" = "+ (num1 + num2));
break;
case "2":
Console.WriteLine("Your result: "+ num1+ " - "+num2 + " = " + (num1 - num2));
break;
}
}
}
Once you are done, you can click on F5 to build and run the project.
You will see output like below:
Enter a number
5
Enter Second number
5
Select Operation:
1 - Add
2 - Subtract
1
Your result: 5 + 5 = 10
In the above code, we have asked the user to add 2 numbers 1 by 1.
Then We ask the user to select any 1 operation, addition, or subtraction.
Once the option is selected we are using Switch
case to select the correct user option and print output by adding or subtracting numbers.
You may also like to read:
Simple C# code examples with output (Console application example)Connect to SQL Server in C# (example using Console application)
How to read JSON data in C# (Example using Console app & ASP.NET MVC)?
Clear Text File or Delete File in C#
Iterate Over Dictionary in C# (Various ways)
Catch Multiple Exceptions in C#
What is Console.Log Equivalent in C#?
Encrypt password & decrypt it (C# Console application example)