C# 中的 return 语句到底做了什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15237649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What exactly does a return statement do in C#?
提问by Mikey D
It's hard for me to grasp what exactly a return statement is doing. For instance, in this method...
我很难理解 return 语句到底在做什么。例如,在这种方法中...
public int GivePoints(int amount)
{
Points -= amount;
return amount;
}
Even if I place any random integer after return, the GivePoints method still does the exact same thing. So what is it that the return statement is doing?
即使我在返回后放置任何随机整数,GivePoints 方法仍然执行完全相同的操作。那么return语句在做什么呢?
回答by Inisheer
In you example, the function is returning the exact number you are sending to it. In this case, whatever value you pass as amount
. Therefore, the return in your current code is a bit pointless.
在您的示例中,该函数返回您发送给它的确切数字。在这种情况下,无论您作为amount
. 因此,当前代码中的返回有点毫无意义。
So in your example:
所以在你的例子中:
int x = GivePoints(1000);
x will equal 1000
x 将等于 1000
回答by chris-mv
Return will exit the function when calling it. Whatever is below the return statement will thus not be executed.
Return 将在调用时退出该函数。因此,不会执行 return 语句下方的任何内容。
Basically, return
indicates that whatever operation the function was supposed to preform has been preformed, and passes the result of this operation back (if applicable) to the caller.
基本上,return
表示该函数应该执行的任何操作都已执行,并将此操作的结果(如果适用)传递回调用者。
回答by Travis J
return
will return control from the current method to the caller, and also pass back whatever argument is sent with it. In your example, GivePoints
is defined to return an integer, and to accept an integer as an argument. In your example, the value returned is actually the same as the argument value.
return
将控制权从当前方法返回给调用者,并将随它发送的任何参数传回。在您的示例中,GivePoints
被定义为返回一个整数,并接受一个整数作为参数。在您的示例中,返回的值实际上与参数值相同。
A returned value is used from somewhere else in your code that calls the defined method, GivePoints
in this example.
GivePoints
在此示例中,从调用已定义方法的代码中的其他位置使用返回值。
int currentPoints = GivePoints(1);
would mean that currentPoints
gets assigned the value of 1.
将意味着currentPoints
被分配的值为 1。
What this breaks down to is that GivePoints
is evaluated. The evaluation of GivePoints
is based on what the method returns. GivePoints
returns the input, thus, GivePoints
will evaluate to 1 in the above example.
这分解为GivePoints
被评估的。的评估GivePoints
基于方法返回的内容。GivePoints
返回输入,因此,GivePoints
在上面的例子中将评估为 1。
回答by Travis J
just a guess for your original goal
只是对你最初目标的猜测
public int GivePoints(int amount)
{
Points -= amount;
return Points;
}
so return will return the updated value of Points
所以 return 将返回 Points 的更新值
if this is not your case the code should be
如果这不是你的情况,代码应该是
public void GivePoints(int amount)
{
Points -= amount;
}
回答by Morris S
Return
will always exit (leave) the function, anything after return will not execute.
Return
将始终退出(离开)函数,返回后的任何内容都不会执行。
Return example:
返回示例:
public int GivePoints(int amount)
{
Points -= amount;
return; //this means exit the function now.
}
Return a variable example:
返回一个变量示例:
public int GivePoints(int amount)
{
Points -= amount;
return amount; //this means exit the function and take along 'amount'
}
Return a variable example and catch the variable that got returned:
返回一个变量示例并捕获返回的变量:
public int GivePoints(int amount)
{
Points -= amount;
return amount; //this means exit the function and take along 'amount'
}
int IamCatchingWhateverGotReturned = GivePoints(1000); //catch the returned variable (in our case amount)