Int32 - InRange
A T extension method that check if the value is between inclusively the minValue and maxValue.
public static void Main() { Int32 searchVal = 7; Int32 minVal = 7; Int32 maxVal = 32; // C# Extension Method: Int32 - 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 Int32 @this, Int32 minValue, Int32 maxValue) { return @this.CompareTo(minValue) >= 0 && @this.CompareTo(maxValue) <= 0; } }