Suppose you have a C # method with an operator returninside a block try. Is it possible to change the return value in a block finally?
An obvious approach, using returnin a block finallywill not work:
String Foo()
{
try
{
return "A";
}
finally
{
return "B";
}
}
Interestingly, this can be done in VB: returninternally it is finallyforbidden, but we can abuse the fact that (perhaps for reasons of backward compatibility) VB still allows you to change the return value by assigning its method name:
Function Foo() As String ' Returns "B", really!
Try
Return "A"
Finally
Foo = "B"
End Try
End Function
Notes:
Please note that I ask this question solely out of scientific curiosity; obviously, such code is very error prone and confusing and should never be written.
, try { var x = "A"; return x; } finally { x = "B"; }. , , , . , , try, finally.