|
INTERVIEW QUESTIONS
MICROSOFT
C#
DETAILS
Question: If I return out of a try/finally in C#, does the code in the finally-clause run?
Answer: 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; class main { public static void Main() { try { Console.WriteLine("In Try block"); return; } finally { Console.WriteLine("In Finally block"); } } } 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 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).
|
|
|
Category |
C# Interview Questions & Answers -
Exam Mode /
Learning Mode
|
Rating |
(0.3) By 8101 users |
Added on |
10/24/2009 |
Views |
76740 |
Rate it! |
|
|
Question:
If I return out of a try/finally in C#, does the code in the finally-clause run?
Answer:
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; class main { public static void Main() { try { Console.WriteLine("In Try block"); return; } finally { Console.WriteLine("In Finally block"); } } } 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 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). Source: CoolInterview.com
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|
|
Related Questions |
View Answer |
|
Does C# support try-catch-finally blocks?
|
View Answer
|
|
Is it possible to have a static indexer in C#?
|
View Answer
|
|
What optimizations does the C# compiler perform when you use the /optimize+ compiler option?
|
View Answer
|
|
How can I access the registry from C# code?
|
View Answer
|
|
Does C# support #define for defining global constants?
|
View Answer
|
|
Is there any sample C# code for simple threading?
|
View Answer
|
|
Is there regular expression (regex) support available to C# developers?
|
View Answer
|
|
Why do I get a security exception when I try to run my C# app?
|
View Answer
|
|
Does C# support parameterized properties?
|
View Answer
|
|
How do you inherit from a class in C#?
|
View Answer
|
|
Does C# support multiple inheritance?
|
View Answer
|
|
Describe the accessibility modifier protected internal.
|
View Answer
|
|
C# provides a default constructor for me. I write a constructor that takes a string as a parameter, but want to keep the no parameter one. How many constructors should I write?
|
View Answer
|
|
What is a delegate?
|
View Answer
|
|
What is a multicast delegate?
|
View Answer
|
|
What are the ways to deploy an assembly?
|
View Answer
|
|
What is a satellite assembly?
|
View Answer
|
|
What namespaces are necessary to create a localized application?
|
View Answer
|
|
What is the difference between // comments, /* */ comments and /// comments?
|
View Answer
|
|
How do you generate documentation from the C# file commented properly with a command-line compiler?
|
View Answer
|
Please Note: We keep on updating better answers to this site. In case you are looking for Jobs, Pls Click Here Vyoms.com - Best Freshers & Experienced Jobs Website.
View All C# Interview Questions & Answers - Exam Mode /
Learning Mode
|