c# - Breaking a foreach vs condition check in for -


in terms of better coding practices, better decision?

  1. using break in foreach jump out of loop
  2. using condition check in loop end loop

so example foreach:

foreach(string bean in stringarray) {     if(bean == "green")     {         break;     } } 

and example loop:

bool found = false; (int = 0; < stringarray.length && !found; i++) {     if (stringarray[i] == "green")     {         found = true;     } } 

just abstract away mechanism entirely method.

(in case, .net has such method, don't need write one.)

if want determine if there item meeting condition:

var found = stringarray.any(bean => bean == "green"); 

if want of items until condition met:

var query = stringarray.takewhile(bean => bean != "green"); 

or if want act on each of said items:

foreach(var bean in stringarray.takewhile(bean => bean != "green")) {  } 

Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -