How to exit using statement

I am trying to exit the using statement while remaining in a closed loop. eg.

for (int i = _from; i <= _to; i++) { try { using (TransactionScope scope = new TransactionScope()) { if (condition is true) { // I want to quit the using clause and // go to line marked //x below // using break or return drop me to line //y // outside of the for loop. } } } //x } //y 

I tried using break, which splashes me out on // y, however I want to stay in the for loop in // x so that the for loop continues to process. I know I can do this by throwing an exception and using the catch, but I would prefer not to do this relatively expensive operation if there is a more elegant way to get out of use. Thanks!

+7
source share
8 answers

As @Renan said, you can use the operator ! and invert your bool result provided. You can also use continue C # keyworkd to go to the next element 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      }    }  } } 
+3
source

Skip full use:

 if (condition is false) { using (TransactionScope scope = new TransactionScope()) { .... 
+8
source

There is no need to break out of the using block because the block used is not working. You can just fail to the end. If there is code that you do not want to execute, skip it using if -clause.

  using (TransactionScope scope = new TransactionScope()) { if (condition) { // all your code that is executed only on condition } } 
+4
source

Just change the if so that you enter the block if the condition is NOT true. Then the rest of the code is inside this block.

+3
source

I would give up logic and say:

 for (int i = _from; i <= _to; i++) { try { using (TransactionScope scope = new TransactionScope()) { if (condition is false) { // in here is the stuff you wanted to run in your using } //having nothing out here means you'll be out of the using immediately if the condition is true } } //x } //y 

On the other hand, if you completely miss the use, as Dave Bish suggests, your code will work better, because in cases where you do not want to use, you will not create an object, you simply will not do anything with it ..

+1
source

I wonder why you want to create transaction areas in a for loop? It's necessary? Could cause a possible escalation of the DTC? Why not?

  using (TransactionScope scope = new TransactionScope()) { for (int i = _from; i <= _to; i++) { if (condition) { // Do the do continue; } } } 
0
source

I'm just wondering the same thing. None of the above answers fits my case. Then I realized this:

Exceptions are usually a sign that something has gone wrong. They can also be useful for basic flow control.

 for (int i = _from; i <= _to; i++) { try { using (TransactionScope scope = new TransactionScope()) { if (condition is true) { throw new Exception("Condition is true."); } } } catch(Exception exception) { Console.WriteLine(exception.Message); }//x } //y 
0
source

Have you tried using

 continue; 

?

-1
source

Source: https://habr.com/ru/post/945173/


All Articles