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

javascript - three.js lot of meshes optimization -

smartface.io - Proper way to change color scheme for whole application -

Email notification in google apps script -