How to exit from a using statement
Skip the using completely:
if (condition is false)
{
using (TransactionScope scope = new TransactionScope())
{
....
As @Renan said you can use !
operator and invert your bool result on the condition. You also can use the continue
C# keyworkd to go to next item of your loop.
for (int i = _from; i <= _to; i++)
{
try
{
using (TransactionScope scope = new TransactionScope())
{
if (condition is true)
{
// some code
continue; // go to next i
}
}
}
}
There is no need to break out of a using
block because a using block does not loop. You can simply fall through to the end. If there is code you don't want to execute, skip it using an if
-clause.
using (TransactionScope scope = new TransactionScope())
{
if (condition)
{
// all your code that is executed only on condition
}
}