Console Application in C#
The article describe how to create a Console Application in C#?Summary of the article:
- What is Console Application?
- How to Read in Console Application?
- How to Write in Console Application?
- How to Prevent auto Closing the Console Window?
- Creation a Console Application
Here we will make a simple console application which will take two integer as input and will show their sum as output.
What is Console Application?
An application that runs in a console window is Console application. It performs all their input and output at the command line same as a C/C++ program. It doesn’t have any graphical user interface.
An application that runs in a console window is Console application. It performs all their input and output at the command line same as a C/C++ program. It doesn’t have any graphical user interface.
How to Read in Console Application
C# code to read form the Console Window is given bellow:
C# code to read form the Console Window is given bellow:
Console.ReadLine(); Console.Read();
How to Write in Console Application
The C# code to write in Console Window is given bellow:
The C# code to write in Console Window is given bellow:
console.Write("Enter your text"); //Writes values to the console window. Console.WriteLine("Enter your text"); //Writes values to the console window with new line.
How to Prevent auto Closing the Console Window?
When we run a console application from the debugger it might automatically close the console window when the application ends. To prevent this we need to make the console application to wait until we press a key. For this please use any one of the following 3 methods at the end of the main method .
When we run a console application from the debugger it might automatically close the console window when the application ends. To prevent this we need to make the console application to wait until we press a key. For this please use any one of the following 3 methods at the end of the main method .
Console.Read(); Console.ReadLine(); Console.ReadKey();
Creation a Console Application
The steps to Create a console application
Step 1
Open Visual Studio ->File -> New Project ->Visual C#-> select Console Applications
The steps to Create a console application
Step 1
Open Visual Studio ->File -> New Project ->Visual C#-> select Console Applications
Step 2
Replace the Main() function or method by the following codes.
Replace the Main() function or method by the following codes.
static void Main(string[] args) { int a, b, c; Console.WriteLine("Enter Two Integers"); a = int.Parse(Console.ReadLine()); b = int.Parse(Console.ReadLine()); c = a + b; Console.WriteLine("Sum Of {0} And {1} Is {2}", a, b, c); Console.Read(); }
Step 3
Run the application by Pressing F5 key and give two integer values. It will calculate their sum.
Run the application by Pressing F5 key and give two integer values. It will calculate their sum.
Output
Enter Two Integers
2
5
Sum Of 2 And 5 Is 7
Enter Two Integers
2
5
Sum Of 2 And 5 Is 7
That’s all about C# console application.
No comments