我需要知道这段代码中关键字返回的功能 [英] I need to know the function of keyword return in this code

查看:59
本文介绍了我需要知道这段代码中关键字返回的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我是初学者
我需要在此代码中知道关键字return的功能

hii every one ,i''m beginner
i need to know the function of keyword return in this code

catch (IOException exc)
       {
           Console.WriteLine(exc.Message);
           return;
       }

推荐答案

它的作用与return关键字ALWAYS的作用相同.它将在相关函数中结束执行.
It does what the return keyword ALWAYS does. It ends execution in the function in question.


实际上,这取决于代码的继续方式. 返回将始终退出当前方法(在这种情况下为void类型,例如,等同于帕斯卡中的过程).在某个地方放置一个返回值可以使该方法的代码更易于阅读和理解(并减少运行的机器代码),因为如果不这样做,则必须使用许多if.必须意识到,如果我们从一个块返回,则创建的对象必须被处置,但在返回之前不被处置,直到GC到达它们之前,它们都将保持活动状态.与非托管代码进行交互时,这可能会导致问题甚至内存泄漏.从块内部返回不符合结构化方法(如goto),但经常实现和使用.甚至goto都是用c#实现的,但很少使用-因为它使代码更难理解.
Actually it depend on how the code continues. A return will always exit the current method (of void type in this case, equivalent of procedure in pascal for example). Placing a return somewhere can make the code of the method easier to read and understand (and less machine code to run), because without this a number of ifs has to be used. One has to be aware, that if we return from a block that created objects that has to be disposed, but are not disposed before return, will be alive until the GC reaches them. This can cause problems and even memory leaks when interacting with unmanaged code. Returning from inside the block does not conform with structured methodology (like goto), but is implemented and used often. Even goto is implemented in c# but rarely used - since it makes code harder to understand.


return是一个关键字,它导致您的方法的执行在那里结束,然后控制返回调用它的方法,就好像执行已到达该方法的结尾}"一样.

您可以通过在return语句后指定它来返回参数.

例如:
return is a keyword which causes execution of your method to end there and then, and control to go back to the method which called it, just as if execution had reached the closing ''}'' of the method.

You can return a parameter by specifying it after the return statement.

For example:
private void MyMethod(int value)
    {
    if (value < 0)
        return;
    Console.WriteLine(value);
    }

如果使用正数或零参数调用MyMethod,它将打印该值.
如果使用负值调用它,则不会打印任何内容.

If you call MyMethod with a positive or zero parameter, it will print the value.
If you call it with a negative value it will not print anything.


这篇关于我需要知道这段代码中关键字返回的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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