Here is the code extracted of the SingleOrDefault function:
public static TSource SingleOrDefault<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate) {
if (source == null) throw Error.ArgumentNull("source");
if (predicate == null) throw Error.ArgumentNull("predicate");
TSource result = default(TSource);
long count = 0;
foreach (TSource element in source) {
if (predicate(element)) {
result = element;
checked { count++; }
}
}
switch (count) {
case 0: return default(TSource);
case 1: return result;
}
throw Error.MoreThanOneMatch();
}
I'm wondering to know if there is any reason why after finding more than one element in the loop, there is no break statement to prevent looping the rest of the list. In anyways, an error will occurs. For a big list where more than one item is found at the beginning, I think it would make a uge difference.