|
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: 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.2) By 8133 users |
Added on |
9/22/2014 |
Views |
66610 |
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: 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 |
|
If I return out of a try/finally in C#, does the code in the finally-clause run?
|
View Answer
|
|
Is it possible to have a static indexer in C#?
|
View Answer
|
|
Is it possible to have different access modifiers on the get/set methods of a property in C#?
|
View Answer
|
|
Is it possible to inline assembly or IL in C# code?
|
View Answer
|
|
What are the rules to be followed while naming variables in C#?
|
View Answer
|
|
Explain about the rules for naming classes in C#?
|
View Answer
|
|
Explain about C#?
|
View Answer
|
|
C# constructors cannot be inherited.Why?
|
View Answer
|
|
Why we can not do the declaration in the class?
|
View Answer
|
|
What is delegates ?
|
View Answer
|
|
How to create and manage files in c#?
|
View Answer
|
|
Is JIT compiler in .Net is vendor specific?i mean ,can we download it and use in other any language compiler?
|
View Answer
|
|
Give me Change syntax of color change (1)Background color (2)font color (3)size of font
|
View Answer
|
|
What is difference between deep copy & sallow copy?
|
View Answer
|
|
What is Thread? Use of Threads in c#.
|
View Answer
|
|
What is server code and client code?
|
View Answer
|
|
Why we using in header file using system and more header file is in C# what is difference between and all header file ?
|
View Answer
|
|
Real life examples of polymorphisms, encapsulation and inheritance?
|
View Answer
|
|
What is partial class. & what is advantage.
|
View Answer
|
|
What is the name of c#.net 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
|