C# console based application
Console programs are easy to develop, because console based applications perform all their input and output at the command line, they are ideal for quickly trying out language features and writing command-line utilities. If you have ever learned a programming language, you know that they all start with the "Hello, world!" program. Here also we start with the "Hello, world!".
Hello World - Your First Program
In order to create a C# console based application
1. Open your Visual Studio
2. On the File menu, click New Project.
Then the New Project dialog box appears. This dialog box lists the different default application types.
3. Select Console Application as your project type and change the name of your application at the bottom textbox.
If you are npt comfortable with default location, you can always enter a new path if you want.
4. Then Click OK.
After click OK button, you will get a screen like the following picture.
Here in the following program, we just print a "Hello, world!" message only. So copy and paste the following command in the main code block.
Here you can see the full source code.
After enter the command, the next step is to run the program. You can run your program using Ctrl+F5 . Then Visual Studio will keep the console window open, until you press a key. You will get screen look like the following picture.
Now you created your first program in Visual Studio.
How to stop C# console applications of closing automatically
The first program is to run your application by using Ctrl+F5, so the console window will remain open until you press a key. The disadvantage of this (Ctrl+F5) is that you lose Visual Studio's debug information. If you run your console application by using F5 instead of Ctrl+F5 , you can see the window will disappear suddenly. It is because you program is finished. When console applications have completed executing and return from their main method, the associated console window automatically closes. This is expected behavior.
Prevent a C# Console Application from closing when it finishes
If you want to keep the application open for debugging purposes, you'll need to instruct the computer to wait for a key press before ending the app and closing the window. In this case you can use Console.ReadLine() to wait for the user to Enter or Console.ReadKey to wait for any key.
C# Command Line Parameters
In some situations, you have to send command line parameters to your application. When you pass arguments, the command line input, read from the standard entry point as string array.
The arguments of the Main method is a String array that represents the command-line arguments. Usually you determine whether arguments exist by testing the Length property.
The main program accept arguments in the order of args, args etc. The following program shows how to display all parameters enter to the console application.
How to pause console application
System.Threading.Thread.Sleep() will delay your programs. It receives a value indicating the number of milliseconds to wait. This can be useful for waiting on an external application or task.
When you run the above program, it will wait for 5 seconds and exit from application.
How do I correctly exit from a console application
A console application will just terminate when it's done, generally when it runs to the end of its Main entry point method. A "return" will achieve this.Full Source C#
Console based applications will exit when the main function has finished running, though you must be careful to get rid of any lingering resources you might have been managing, as they can keep the underlying process alive. In some situations you may have to forcefully terminate your program. In this case you can explicitly exit using Environment.Exit.
If you're returning an error code you can do it this way, which is accessible from functions outside of the initial thread: