Saturday, October 25, 2008

What does the return in main method actually do

sample c# program

class MainReturnValTest
{
static int Main()
{
//...
return 0;
}
}


a batch file is used to invoke the executable resulting from the previous code example. Because the code returns zero, the batch file will report success, but if the previous code is changed to return a non-zero value, and is then re-compiled, subsequent execution of the batch file will indicate 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 Succeded
echo return value = %ERRORLEVEL%
goto end

:end

No comments: