I don't really understand how the instructions flow in the following code.
The body of finally
is guaranteed to be executed before the method returns. If so, the return value should be 0 rather than 1.
Could you explain the internal mechanism why the return value is still 1 even though the finally
has reset it to 0?
class Container
{
int data = 0;
public int Retrieve()
{
try
{
Inc();
return data;
}
finally
{
Reset();
//return data;
}
}
void Reset()
{
data = 0;
WriteLine("Reset");
}
void Inc() => data++;
}
class ReturnInTry
{
static void Main()
{
Clear();
WriteLine("Start");
WriteLine(new Container().Retrieve());
WriteLine("End");
}
}