I just want to know is it safe/ good approach to call return inside a using block.
For ex.
using(var scope = new TransactionScope())
{
// my core logic
return true; // if condition met else
return false;
scope.Complete();
}
We know the at the last most curly brace dispose() will get called off. But what will be in the above case, since return jumps the control out of the given scope (AFAIK)…
- Is my
scope.Complete()get called? - And so for the scope’s
dispose()method.
It’s perfectly safe to call
returninside yourusingblock, since a using block is just atry/finallyblock.In your example above after return
true, the scope will get disposed and the value returned.return false, andscope.Complete()will not get called.Disposehowever will be called regardless since it reside inside the finally block.Your code is essentially the same as this (if that makes it easier to understand):
Please be aware that your transaction will never commit as there’s no way to get to
scope.Complete()to commit the transaction.