Answer: </P><P align=justify>Yes. The code in the finally always runs. If you return out of the try block, or even if you do a "goto" out of the try, the finally block always runs, as shown in the following example: using System;</P><P align=justify>class main </P><P align=justify>{</P><P align=justify>public static void Main() </P><P align=justify>{</P><P align=justify>try </P><P align=justify>{</P><P align=justify>Console.WriteLine("In Try block");</P><P align=justify>return;</P><P align=justify>}</P><P align=justify>finally </P><P align=justify>{</P><P align=justify>Console.WriteLine("In Finally block");</P><P align=justify>}</P><P align=justify>}</P><P align=justify>}</P><P align=justify></P><P align=justify>Both "In Try block" and "In Finally block" will be displayed. Whether the return is in the try block or after the try-finally block, performance is not affected either way. The compiler treats it as if the return were outside the try block anyway. If it's a return without an expression (as it is above), the IL emitted is identical whether the return is </P><P align=justify>inside or outside of the try. If the return has an expression, there's an extra store/load of the value of the expression (since it has to be computed within the try block).
Tags:Interview Questions, Microsoft, C#,
Please submit your Interview Answer for the above Question via replying to this post. Thank you for visiting to our blog, Have a Good Day!.
Sponsor Advertisement
Library Management System Software
Our Library Management Softare offers complete library management modules and automation solution that enables information providers, information managers, resource incharge, resource managers & librarians to manage & disseminate information & resources....
No comments:
Post a Comment