ARTICLE AD BOX
I have a question about how try/finally behaves when using return statements in C#.
Consider the following methods:
public int Test() { return 12; } public int Test2() { try { return 12; } finally { Console.WriteLine("Test"); } }Question 1:
When calling Test2() , will the Console.WriteLine inside the finally block execute even though the method already hits the return inside the try ?
Now consider this version:
public int Test2() { try { return 12; } finally { return 23; } }Question 2:
If both try and finally contain a return statement, which value is actually returned? Does the return inside the finally override the return value from the try block?
What I expect to understand:
The exact order of execution when returning inside try
Whether the finally block always run
(my main question) What the final return value is when both blocks contain return
Thanks in advance!
