究竟什么是一个return语句在C#中吗? [英] What exactly does a return statement do in C#?

查看:161
本文介绍了究竟什么是一个return语句在C#中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我很难把握究竟return语句在做什么。例如,这种方法...

It's hard for me to grasp what exactly a return statement is doing. For instance, in this method...

    public int GivePoints(int amount)
    {
        Points -= amount;
        return amount;
    }



即使我把回归后的任何随机整数,该GivePoints方法仍对同样的事情。那么到底是什么return语句是干什么的?

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 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.

一个返回值是从别的地方在你的代码调用定义的方法,<$使用。C $ C> GivePoints 在这个例子

A returned value is used from somewhere else in your code that calls the defined method, GivePoints in this example.

int currentPoints = GivePoints(1);



意味着 currentPoints 被分配的值1。

这是什么打破了的是, GivePoints 进行评估。 GivePoints的评价基于该方法返回的东西。 GivePoints 返回输入,因此, GivePoints 将计算为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.

这篇关于究竟什么是一个return语句在C#中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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