Breaking out of loops
What annoys some programmers and reduces the performance of the application is that the loop in the preceding code continues, even when the student has been found. We can improve the indentation and performance of the preceding code as follows:
public Student Find(List<Student> list, int id){ Student r = null; foreach (var i in list) { if (i.Id == id) { r = i; break; } } return r; }
In the preceding code, we have improved the formatting and made sure that the code is properly indented. We’ve added a break
statement to the for
loop so that the...