Return 0 Value From C# Entry Point Method Using Parent and Child Programs
In this post, we check out one developer's experimental code, and why he believes it can help fellow devs. Read on to learn more!
Join the DZone community and get the full member experience.Join For Free
Hello everyone, as we know that in C# apps (both Console and Win) 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. This
Main() does have a return type of “int” and much of the time we noticed that the return value for this
Main() seems to 0. So let’s understand the behind the scenes of this return value.
In general, our assumption is (after a Google search) a return of 0 in
Main() refers to the status code of the application such as, “Exited Successfully,” or if it's 1. then it exited with some errors. But, these 0/1 values are user-defined.
The Real Benefit of Using Return 0
If a console application/windows application ends its execution by default, GC will perform clean up all of the allocated resources with respect to the applications. What is the purpose of returning 0 to the OS by the
Main() method? Is it something related with flag bits? Or something where a kind of process ends? The answer is no. Because the OS never minds the return value from
Main(), it always considers the particular application to have been completed successfully. Okay, then what is the use of returning 0? Yes, here is where the role of something like Parent and Child Processes come into place.
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 then 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 program execution? This can be validated by the use of “Exit Code,” which is returned by
Main(). This only exits code (we are assuming) as a flag bit. Let me show this Parent and Child Program with a simple example.
Considering 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).
Sample Demo Program
- Open Visual Studio (any version), and create a new project for a Windows Application.
- Once the project is created, please add another project on top of the above Windows Application.
- The final Project structure looks like below:
4. In the “WindowsAppDemo” project, Add a LabelBox, TextBox, and Command button as shown below:
5. Add the following code to the button click event, in order to call the Console App which is available in this project solution.
In the above code, first, we have assigned
ChildAppDemo (Console App) to
ProcessStartInfo, which is holding the information about the target app.
6. Now, go to the “ChildAppDemo” console app, and receive the passing parameter by Windows App and print it on the console (as shown below).
7. Currently, our project setup so: We have a Parent program (Windows App) which is internally calling the Child Program (Console App). Make the Parent program (Windows App) a startup project and debug the code to see the return value. Let me show you here.
- Once you executed the Windows App, just provide some text and click on the Command Button. Look at the breakpoint here. The Child program is called and is still running; meanwhile, if you look at the breakpoint on the called process class, it's showing properties such as “Has Exited” and the Exit Code values seems to be unavailable.
- Hit enter on the console app. As we wrote
Console.ReadKey()so the Child App (Console app) remains running, to go back to the Parent Program (Windows App). Just hit enter on the Console App. Do not press any key except enter. Again look at the breakpoint, this time it will be getting the exit code as a 0. This is the actual benefit of having return values to
- Final output:
I've tried to share the details about having return 0 in the
Main() method. This is just an experimental program. It can be used assuming we have cascade console apps in a solution and it has been called, one-by-one, meaning we can validate the called cascading projects by using the “Exit Code” property.
Thanks much for reading this article.
Opinions expressed by DZone contributors are their own.