How to execute certain part of code before/after return was called. For example return may be called multiple times in a method therefore I wish not to copy paste same lines before return.
I might be wrong but some people told me that try block makes code run slow or so and my method is being called over 1000 times therefore even though this task could be accomplished with try/finally block I would like to avoid it.
Example:
void method()
{
MyObject activator = new ...
AnotherObject something = new ...
SomethingElse asdf = new ...
// some other variables
if(..)
// Deactivate activator, close things, confirm user exited
// Do some post calculations
return;
if(..)
// Deactivate activator, close things, confirm user exited
// Do some post calculations
return;
if(..)
// Deactivate activator, close things, confirm user exited
// Do some post calculations
return;
}
Now I need to execute same code before or after every return. I the code I need to use variables defined at top in the method that's why I cannot outsource. How to do this? Is there a way at all? I apologize in case of a duplicate.