// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. using System; using System.Collections.Generic; namespace Microsoft.MixedReality.Toolkit { /// /// Extension methods for the .Net IEnumerable class /// public static class EnumerableExtensions { /// /// Returns the max element based on the provided comparer or the default value when the list is empty /// /// Max or default value of T public static T MaxOrDefault(this IEnumerable items, IComparer comparer = null) { if (items == null) { throw new ArgumentNullException("items"); } comparer = comparer ?? Comparer.Default; using (var enumerator = items.GetEnumerator()) { if (!enumerator.MoveNext()) { return default(T); } var max = enumerator.Current; while (enumerator.MoveNext()) { if (comparer.Compare(max, enumerator.Current) < 0) { max = enumerator.Current; } } return max; } } } }