Min.cs 1.2 KB

123456789101112131415161718192021222324252627282930
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. namespace System.Linq
  6. {
  7. public static partial class EnumerableEx
  8. {
  9. #if !(REFERENCE_ASSEMBLY && (NET6_0))
  10. /// <summary>
  11. /// Returns the minimum value in the enumerable sequence by using the specified comparer to compare values.
  12. /// </summary>
  13. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  14. /// <param name="source">Source sequence.</param>
  15. /// <param name="comparer">Comparer used to determine the minimum value.</param>
  16. /// <returns>Minimum value in the sequence.</returns>
  17. public static TSource Min<TSource>(this IEnumerable<TSource> source, IComparer<TSource> comparer)
  18. {
  19. if (source == null)
  20. throw new ArgumentNullException(nameof(source));
  21. if (comparer == null)
  22. throw new ArgumentNullException(nameof(comparer));
  23. return MinBy(source, x => x, comparer).First();
  24. }
  25. #endif
  26. }
  27. }