什么返回?在main()函数中意味着什么? [英] What does return; mean in main() function?

查看:105
本文介绍了什么返回?在main()函数中意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有点困惑.
下例中main()函数中return;的含义/目的是什么.

示例C#代码.

Hi,
I have a little confusion.
What is the meaning/purpose of return; in main() function in below example.

Sample C# code.

static void Main(string[] args){
     .... some code
     return;   // What does this mean??

}



在此先感谢.



Thanks in Advance.

推荐答案

此示例可以帮助您了解可能需要它的地方:

This example may help you understand where it might be needed:

static void Main(string[] args)
{
  // some code here
 
  if(someFlag) // exit if flag is set
  {
    return;
  }
 
  // some more code here
 
}


由于main()返回int,因此它需要一个return语句,并且在其后应具有适当类型的值. br/>
去这里:

http://msdn.microsoft.com/en-us/library/0fwzzxz2 (v = vs.80).aspx [
Since main() returns an int, it needs a return statement, and it should have a value after it that is of the appropriate type.

Go here:

http://msdn.microsoft.com/en-us/library/0fwzzxz2(v=vs.80).aspx[^]

BTW, in .Net 4, when you create a new console app, the return type in main() is void, so no return statement is needed. I think you can change the return type though, but I''ve never tried it.

Calling return; in a method that doesn''t return a value doesn''t serve any purpose. You can safely remove it.


这意味着程序将退出.由于main被声明为返回int(程序退出代码),因此return实际上应该类似于return 1;.可以由启动您的可执行文件的程序评估此返回码或程序退出代码,以检查其是否正常运行或错误是否导致程序终止.

Main也称为程序入口点.

希望能有所帮助.

干杯!

-MRB
It means that the program will exit. Since main is declared as returning an int (program exit code) return should really be something like return 1; instead. This return code or program exit code can be evaluated by programs that started your executable to check if it operated correctly or if an error had caused program termination.

Main is also called a program entry point.

Hope that helps a bit.

Cheers!

-MRB


这篇关于什么返回?在main()函数中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆