예제 #1
0
        /// <summary>
        /// Gets the maximum value in <paramref name="_this"/>.
        /// </summary>
        /// <typeparam name="T">The type of the objects to compare.</typeparam>
        /// <param name="_this">The <see cref="IEnumerable{T}"/> that is used.</param>
        /// <param name="comparer">The <see cref="IComparer"/> used for comparison.</param>
        /// <returns>The maximum value in <paramref name="_this"/>.</returns>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="_this"/> is null.</exception>
        /// <exception cref="ArgumentException">Thrown if <paramref name="_this"/> is empty.</exception>
        /// <exception cref="ArgumentException">Thrown if <paramref name="_this"/> contains null values only.</exception>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="comparer"/> is null.</exception>
        /// <example>
        /// <code>
        /// var min = myEnumeration.Max(new MyComparer());
        /// </code>
        /// </example>
        public static T Maximum <T>(this IEnumerable <T> _this, IComparer comparer)
        {
            Throw.If.Object.IsNull(_this, nameof(_this));
            Throw.If.Object.IsNull(comparer, nameof(comparer));

            return(_this.Maximum(DynamicComparer.FromComparer <T>(comparer)));
        }
예제 #2
0
        /// <summary>
        /// Gets the minimum value in <paramref name="_this"/>.
        /// </summary>
        /// <typeparam name="T">The type of the objects to compare.</typeparam>
        /// <param name="_this">The <see cref="IEnumerable{T}"/> that is used.</param>
        /// <param name="comparer">The <see cref="IComparer"/> used for comparison.</param>
        /// <returns>The minimum value in <paramref name="_this"/>.</returns>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="_this"/> is null.</exception>
        /// <exception cref="ArgumentException">Thrown if <paramref name="_this"/> is empty.</exception>
        /// <exception cref="ArgumentException">Thrown if <paramref name="_this"/> contains null values only.</exception>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="comparer"/> is null.</exception>
        /// <example>
        /// <code>
        /// var min = myEnumeration.Min(new MyComparer());
        /// </code>
        /// </example>
        public static T Minimum <T>(this IEnumerable <T> _this, IComparer comparer)
        {
            Throw.If.Object.IsNull(_this, nameof(_this));
            Throw.If.Value.IsFalse(_this.Count() > 0, nameof(_this), "The enumeration is empty.");
            Throw.If.Value.IsFalse(_this.Any((element) => element != null), nameof(_this), "The enumeration only contains null values.");
            Throw.If.Object.IsNull(comparer, nameof(comparer));

            return(_this.Minimum(DynamicComparer.FromComparer <T>(comparer)));
        }
예제 #3
0
        void FromComparer()
        {
            IComparer <Dummy> comp   = null;
            Int32             result = 0;

            Test.If.Action.ThrowsException(() => comp = DynamicComparer.FromComparer <Dummy>(null), out ArgumentNullException ex1);
            Test.If.Value.IsEqual(ex1.ParamName, "comparer");

            Test.IfNot.Action.ThrowsException(() => comp = DynamicComparer.FromComparer <Dummy>(DynamicComparer.FromDelegate((x, y) => 42)), out Exception ex2);

            result = comp.Compare(0, 1);
            Test.If.Value.IsEqual(result, 42);
        }