jeudi 4 février 2016

Bracketed if inside non bracketed foreach

This may have been answered somewhere before but I could not figure out a good way to word my search.

foreach(item in list)
  if(something)
  {
    dosomething();
  }
if(somethingunrelated)
  {
    dosomethingunrelated();
  }

Besides the obvious of being extremely hard to read, is there any way that either if statement might be skipped?

What I think is expected here is that the foreach will iterate over every item in the list and apply the first if statement. After the foreach is completed the second if will be applied once.

I did not write this and am scouring to find an issue on a legacy system. I don't think this is it, but I seem to remember that you can ONLY skip brackets if everything nested skips the brackets as well (in c#.net). Is that correct?

Aucun commentaire:

Enregistrer un commentaire