Decimal - InRange
A T extension method that check if the value is between inclusively the minValue and maxValue.
public static void Main() { Decimal searchVal = 7.64m; Decimal minVal = 7.03m; Decimal maxVal = 32.7865m; // C# Extension Method: Decimal - InRange if(searchVal.InRange(minVal, maxVal)) { Console.WriteLine("{0} is in range [{1} --- {2}]", searchVal, minVal, maxVal); } else { Console.WriteLine("{0} is not in range [{1} --- {2}]", searchVal, minVal, maxVal); } }
View Source
using System; public static partial class Extensions { /// <summary> /// A T extension method that check if the value is between inclusively the minValue and maxValue. /// </summary> /// <param name="this">The @this to act on.</param> /// <param name="minValue">The minimum value.</param> /// <param name="maxValue">The maximum value.</param> /// <returns>true if the value is between inclusively the minValue and maxValue, otherwise false.</returns> /// ### /// <typeparam name="T">Generic type parameter.</typeparam> public static bool InRange(this Decimal @this, Decimal minValue, Decimal maxValue) { return @this.CompareTo(minValue) >= 0 && @this.CompareTo(maxValue) <= 0; } }