I have a code like this:
public void Method() { if(something) { //some code if(something2) { now I should break from ifs and go to te code outside ifs } return; } // The code i want to go if the second if is true }
I want to know if it is possible to go to this code after ifs without using the goto statement or retrieving the rest of the code with another method.
Update:
Yes, I know Else;) But this code is long and should be run if the first IF is false and when the first IF is true and the second is false. so I think the extraction method is the best
To answer your question:
public void Method() { while(true){ if(something) { //some code if(something2) { break; } return; } break; } // The code i want to go if the second if is true }
Use else:
else
if(something) { //some code if(something2) { // now I should break from ifs and go to te code outside ifs } else return; } // The code i want to go if the second if is true
goto, - . , thing1 true, 2 .
if (something) { do_stuff(); if (thing1) { do_thing1(); goto SkipToEnd; } if (thing2) { do_thing2(); } SkipToEnd: do_thing3(); }
, . -, C++.
, , , IF. , , 1) 2) , .
do/while while:
do/while
public void Method() { bool something = true, something2 = false; do { if (!something) break; if (something2) break; } while (false); }
do/while while , IF, false . , break.
false
break
public void Method() { if(something) { //some code if(something2) { // now I should break from ifs and go to te code outside ifs goto done; } return; } // The code i want to go if the second if is true done: // etc. }
else:
public void Method() { if(something) { // some code if(something2) { // now I should break from ifs and go to te code outside ifs } else return; } // The code i want to go if the second if is true }
: if/else break, if / if else / else.
if/else
if / if else / else
public void Method() { if(something) { //some code if(!something2) { return; } } // The code i want to go if the second if is true }
public void Method() { if(something) { //some code if(something2) { // The code i want to go if the second if is true } return; } }
return, !something2 else return:
return
!something2
else return
public void Method() { if(something) { //some code if(something2) { //now I should break from ifs and go to te code outside ifs } if(!something2) // or else return; } // The code i want to go if the second if is true }
, , . Msgstr " , , ". , , if.
if
!
if (new Func<bool>(() => { if (something1) { if (something2) { return true; } } return false; })()) { //do stuff }
:
public void Method() { bool doSomethingElse = true; if(something) { //some code if(!something2) { doSomethingElse = false; } } if(doSomethingElse) { // The code i want to go if the second if is true } }
Another way, starting with c # 7.0, is to use local functions. You can call a local function a meaningful name and call it just before the declaration (for clarity). Here is your example rewritten:
public void Method() { // Some code here bool something = true, something2 = true; DoSomething(); void DoSomething() { if (something) { //some code if (something2) { // now I should break from ifs and go to te code outside ifs return; } return; } } // The code i want to go if the second if is true // More code here }
just want to add one more option to update this wonderful how-to list. However, this can be really useful in more complex cases:
try { if (something) { //some code if (something2) { throw new Exception("Weird-01."); // now You will go to the catch statement } if (something3) { throw new Exception("Weird-02."); // now You will go to the catch statement } //some code return; } } catch (Exception ex) { Console.WriteLine(ex); // you will get your Weird-01 or Weird-02 here } // The code i want to go if the second or third if is true