开发者

What is the purpose of return statement in C# programs

What is the purpose of a return statement.

Example:

Class Sample 
{
    public static int Main()
    {
      System.Console.WriteLine("Hello Wo开发者_开发技巧rld");
      return 0;
    } 
}

In the above program what actually is being returned. Is it returning integer value 0 ? Does this help in the flow of program execution

(This might be a stupid question, but I am eager to know.)


The exit code of the application is set to the value returned from Main. This can be used to communicate to the calling application if there was an error or everything succeeded when running the child application.

When running from the console window, the last exit code is set in the environment variable which can be used to control the execution of a batch file for example.

See the following MSDN entry for more info and an example. http://msdn.microsoft.com/en-us/library/0fwzzxz2.aspx


The exit code of an application (return from main) can be queried in the operating system or in batch programs. In windows, it can be processed in a batch using the %errorlevel% environment var.

@echo off
yourpgrogram
echo Your program returned %errorlevel%

would brint out Your program returned 0

hth

Mario


From the Main() method, the value returned will affect the program's exit code. From MSDN:

... returning an integer enables the program to communicate status information to other programs or scripts that invoke the executable file.


return 0 is a convention for no errors in the execution

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜