Created
April 28, 2016 04:19
-
-
Save kuchikios/3c1d2e26472a337e186cca600957cd65 to your computer and use it in GitHub Desktop.
Min, Max, MinMax
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace Samples | |
{ | |
public static class GenericsExtensions | |
{ | |
public static TValue MinMax<TValue>( TValue arg, TValue min, TValue max ) | |
where TValue : IComparable<TValue> | |
{ | |
return Min( min, Max( max, arg ) ); | |
} | |
public static TValue Max<TValue>( TValue arg, TValue max ) | |
where TValue : IComparable<TValue> | |
{ | |
return | |
null == arg ? | |
null == max ? default( TValue ) | |
: 1 == max.CompareTo( arg ) ? max : arg | |
: 1 == arg.CompareTo( max ) ? arg : max; | |
} | |
public static TValue Min<TValue>( TValue arg, TValue min ) | |
where TValue : IComparable<TValue> | |
{ | |
return | |
null == arg ? | |
null == min ? default( TValue ) | |
: 1 == min.CompareTo( arg ) ? arg : min | |
: 1 == arg.CompareTo( min ) ? min : arg; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment