Creating Console Application In C#

What does the console application mean?

A Console Application, in the context of C#, is an application that takes inputs and displays output at a command-line console with access to three basic streams: standard input, standard output, and standard error.
The console application primarily is designed for the following reasons,
  • To provide a simple user interface for applications requiring little or no user interaction, such as samples for learning C# language features and command-line utility programs.
  • Automated testing, which can reuse automation implementation resources.
Console applications don't have any graphical user interface, they will have character-based interfaces.

Creating a Console Application

Step 1
Open visual studio --> under file menu select the option new -->select project 
Step 2
In left side select Templates --> select Visual C# and select the Console Application
Give some name for the application,
Step 3
After creating an application, by default, it will create a Program.cs file.
Step 4
In C# programming the main method is where Program start execution it is the main entry point of the program that executes all the objects and invokes methods to execute.
There can be only one main method in C#. However, the C# main method can be void or int return type. iI must be inside class or struct and must declare with static modifier, it is the main place where the program starts and ends.
The main method can have a parameter and these parameters are known as the ZERO-INDEXED command line argument. And here is the code for some calculator activities:
  1. int num1 = 0;  
  2. int num2 = 0;  
  3. // Ask the user to type the first number.  
  4. Console.WriteLine("Type a number, and then press Enter");  
  5. num1 = Convert.ToInt32(Console.ReadLine());  
  6. // Ask the user to type the second number.  
  7. Console.WriteLine("Type another number, and then press Enter");  
  8. num2 = Convert.ToInt32(Console.ReadLine());  
  9. // Ask the user to choose an option.  
  10. Console.WriteLine("Choose an option from the following list:");  
  11. Console.WriteLine("\ta - Add");  
  12. Console.WriteLine("\ts - Subtract");  
  13. Console.WriteLine("\tm - Multiply");  
  14. Console.WriteLine("\td - Divide");  
  15. Console.Write("Your option? ");  
  16. // Use a switch statement to do the math.  
  17. switch (Console.ReadLine()) {  
  18.     case "a":  
  19.         Console.WriteLine("Your result: {num1} + {num2} = " + (num1 + num2));  
  20.         break;  
  21.     case "s":  
  22.         Console.WriteLine("Your result: {num1} - {num2} = " + (num1 - num2));  
  23.         break;  
  24.     case "m":  
  25.         Console.WriteLine("Your result: {num1} * {num2} = " + (num1 * num2));  
  26.         break;  
  27.     case "d":  
  28.         Console.WriteLine("Your result: {num1} / {num2} = " + (num1 / num2));  
  29.         break;  
  30. }  
  31. // Wait for the user to respond before closing.  
  32. Console.Write("Press any key to close the Calculator console app...");  
  33. Console.ReadKey();  
There are two methods for writing to the console window, which are used extensivly,
  1. Console.Write()
  2. Console.WriteLine()
Write the specified value to the console window. Write() method has 17 overloads as shown below.
This is one of the output methods for console-class in the runtime library. This does the same but adds a newline character at the end of the output using specified format information. WriteLine() method has 18 overloads as shown below. 
This method reads a string text from the console window. This will read input text from the user at the console windows and display the string at console windows when the user presses the enter key.
Switch is a selection statement that chooses a single section to execute from a list of candidates based on a pattern match with the match expression.
Step 5
Click on F5 and execute the project, and follow the console window. The output will be,

Recommended Free Ebook
Similar Articles