I’m in WinForms and before saving changes to the DataBase, I had to check whether the used ErrorProvider holds an error for any of the displayed Controls.
I figured out several ways to do that among them:
-
a simple foreach loop over the ControlContainer:
foreach (Control c in ctrlcontainer) { if (epOrderHeader.GetError(c) != string.Empty) { return true; } } return false; -
Using the List extension Method Exists(Predicate):
return (ctrlcontainer.Exists(c => epOrderHeader.GetError(c) != string.Empty);
Right from the stomache I expected the second to be the fastest, but using the Eqatec Profiler I discovered, that the foreach loop is slightly faster (in my case about 1ms). While this is insignificant, I still wonder why this happens?
How does the compiler translate these methods and why is the first one faster?
It is probably 1 ms slower in your case because
List<T>.ExistscallsFindIndexwhich is implemented in the following way:So this is a little bit more than a simple
foreach.