We know that in C# Apps (both Console and Windows), an Entry Point method refers to the Main() of a program. When the application is started, the main method is the first method that is invoked. Main() does have a return type as “int” and many times, we notice that the return value for this Main() seems 0. So, let’s understand the behind the scene scenario of having this 0 return value in Main().
Background
In general, our assumption is (after Google search) return 0 in Main() refers to the status code of the application, such as “Exited Successfully” or if it is 1, then Exited with some errors. But actually speaking, these 0/1 are user-defined values returning to the called program.
The real benefit of using return 0
A Console App/Windows App ends its execution by default and GC will perform the cleanup on all of the allocated resources with respect to the applications. While returning 0 to the OS by the Main(), what is the purpose being sought by the OS? Is it something related to flag bits? Or some kind of process ends?
This return value can be used when there is a situation when we have Parent and Child programs. Just assuming that a Parent program called a Child Program. Once the Child has completed the execution, the program control in the Parent process will look for the status (just assuming) of what really happened to the Child Process. Was it completed successfully or it was stalled during the program execution? This can be validated by the use of “Exit Code” which is returned by the Main(). We are assuming this exit code only as a flag bit. Let me show this Parent and Child program with a simple example.
Let us consider the Parent program as Windows App and Child as a Console App. Please follow the steps in order to get the “Exit Code” value (return value).






Join the conversation! Your thoughts help the community grow.