From ac8aead71dff9f14e5dac694e4896fff01f2e714 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Sun, 17 Jan 2021 13:21:56 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20IComparable=20Max=20and=20?= =?UTF-8?q?Min?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Implements #23 --- .../ComparableExtensions.cs | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/X10D/src/ComparableExtensions/ComparableExtensions.cs b/X10D/src/ComparableExtensions/ComparableExtensions.cs index bc9cf14..62b40ec 100644 --- a/X10D/src/ComparableExtensions/ComparableExtensions.cs +++ b/X10D/src/ComparableExtensions/ComparableExtensions.cs @@ -162,5 +162,59 @@ namespace X10D.ComparableExtensions { return value.CompareTo(other) <= 0; } + + /// + /// Returns the maximum of two values. + /// + /// The first value. + /// The second value. + /// A type which implements . + /// G + /// if is greater than + /// -or- + /// otherwise. + /// + /// + /// + /// int first = 5; + /// int second = 10; + /// + /// int max = first.Max(second); + /// // max will be 10 + /// + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static T Max(this T value, T other) + where T : IComparable + { + return value.GreaterThan(other) ? value : other; + } + + /// + /// Returns the minimum of two values. + /// + /// The first value. + /// The second value. + /// A type which implements . + /// + /// if is less than + /// -or- + /// otherwise. + /// + /// + /// + /// int first = 5; + /// int second = 10; + /// + /// int min = first.Min(second); + /// // min will be 5 + /// + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static T Min(this T value, T other) + where T : IComparable + { + return value.LessThan(other) ? value : other; + } } }