|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.|
Main() Return Values (C# Programming Guide)
Updated: July 2009
The Main method can return void:
It can also return an int:
If the return value from Main is not used, returning void allows for slightly simpler code. However, returning an integer enables the program to communicate status information to other programs or scripts that invoke the executable file. The following example shows how the return value from Main can be accessed.
In this example, a batch file is used to run a program and test the return value of the Main function. When a program is executed in Windows, any value returned from the Main function is stored in an environment variable called ERRORLEVEL. A batch file can determine the outcome of execution by inspecting the ERRORLEVEL variable. Traditionally, a return value of zero indicates successful execution. The following example is a simple program that returns zero from the Main function. The zero indicates that the program ran successfully. Save the program as MainReturnValTest.cs.
Because this example uses a batch file, it is best to compile the code from a command prompt. Follow the instructions in How to: Set Environment Variables to enable command-line builds, or use the Visual Studio Command Prompt, available from the Start menu under Visual Studio Tools. From the command prompt, navigate to the folder in which you saved the program. The following command compiles MainReturnValTest.cs and produces the executable file MainReturnValTest.exe.
Next, create a batch file to run MainReturnValTest.exe and to display the result. Paste the following code into a text file and save it as test.bat in the folder that contains MainReturnValTest.cs and MainReturnValTest.exe. Run the batch file by typing test at the command prompt.
Because the code returns zero, the batch file will report success. However, if you change MainReturnValTest.cs to return a non-zero value and then re-compile the program, subsequent execution of the batch file will report failure.
rem test.bat @echo off MainReturnValueTest @if "%ERRORLEVEL%" == "0" goto good :fail echo Execution Failed echo return value = %ERRORLEVEL% goto end :good echo Execution succeeded echo Return value = %ERRORLEVEL% goto end :end