4 using System.Collections.Generic;
14 public static T MaxOrDefault<T>(
this IEnumerable<T> items, IComparer<T> comparer = null)
16 if (items == null) {
throw new ArgumentNullException(
"items"); }
17 comparer = comparer ?? Comparer<T>.Default;
19 using (var enumerator = items.GetEnumerator())
21 if (!enumerator.MoveNext())
26 var max = enumerator.Current;
27 while (enumerator.MoveNext())
29 if (comparer.Compare(max, enumerator.Current) < 0)
31 max = enumerator.Current;