返回语句的最后一个块中的代码编译 [英] Code compilation in final block for return statement

查看:60
本文介绍了返回语句的最后一个块中的代码编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的计划。



Below is my program.

class Program
    {
        int ret = default(int);
        static void Main(string[] args)
        {
            Program ob = new Program();
            Console.WriteLine(ob.DoSomeOperation());
            Console.WriteLine("------------------------------------");
            //Console.WriteLine(ob.ret);
            Console.ReadKey();
        }

        public int DoSomeOperation()
        {
            try
            {
                ret = 5;
                //return ret;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                ret = 9;
            }
            return ret;
        }
    }





你可以在try和最终看到里面看到return语句。



如果我在try块中取消注释return语句,DoSomeOperation方法将返回5,相反它将返回9.



请告诉我,如果finally块一直执行,那么为什么它不返回9如果我在try块中取消注释return语句。



You can see the return statement inside try and after finally.

If I uncommented the return statement in try block, DoSomeOperation method will return 5 and on the contrary it will return 9.

Please clarify me if finally block executes all the time, then why it is not returning 9 if i uncommented the return statement in try block.

推荐答案

最后会运行但是你已经在尝试中返回了值。



所以,它这样做



1.设置ret = 5.

2.返回ret是5

3.最后调用ret = 9



最后保证运行。我假设这只是一些测试代码,但如果它无论如何都模仿你的真实代码,你可能需要做一些更改。
Finally will run but you already returned the value in your try.

So, it does this

1. Sets ret = 5.
2. returns ret which is 5
3. Calls finally which sets ret = 9

Finally is guaranteed to run. I assume this is just some test code but if it in anyway mimics your real code you may need to make some changes.


返回(C#参考) [ ^ ]

return (C# Reference)[^]
Quote:

如果return语句是在try块中,finally块(如果存在)将在控制返回到调用方法之前执行。

If the return statement is inside a try block, the finally block, if one exists, will be executed before control returns to the calling method.



因此,它将执行finally块而不是到达最终的重新运行行。 (您的IDE可能显示您检测到无法访问的线路警告)


So, it will execute finally block and not reach the final rerun line. (Your IDE may be showing you have unreachable line detected warning)


是finally块一直执行 - 这就是finally块存在的原因 - 无论如何都要执行 - 异常与否。 ...

我对你的问题没有问题并回答它,但我发现有关语言语法的基础知识可以从书籍或文档中学到更好 - MSDN?

< a href =http://msdn.microsoft.com/en-us/library/zwc8s4fz.aspx> http://msdn.microsoft.com/en-us/library/zwc8s4fz.aspx [< a href =http://msdn.microsoft.com/en-us/library/zwc8s4fz.aspxtarget =_ blanktitle =新窗口> ^ ]
Yes the finally block executes all the time - that's the reason for the finally block to exist - execute anyway - exception or not....
I have no problem with your question and answering it, but I find such basics about language syntax are better learned from a book or documentation - MSDN?
http://msdn.microsoft.com/en-us/library/zwc8s4fz.aspx[^]


这篇关于返回语句的最后一个块中的代码编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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