CSharp examples for System:Int32
Check if a given number is greater then or equal and less then or equal to a range. x >= y && x <= z
using System.Globalization; using System.Collections.Generic; using System.Collections; using System;//from w w w . ja v a2 s. c o m public class Main{ /// <summary> /// /// </summary> /// <param name="item"></param> /// <param name="rangeStart"></param> /// <param name="rangeStop"></param> /// <returns></returns> public static bool IsInRange (object item, IComparable rangeStart, IComparable rangeStop) { if (item == null || rangeStart == null || rangeStop == null) return false; return rangeStart.CompareTo (item) >= 0 && rangeStop.CompareTo (item) <= 0; } /// <summary> /// /// </summary> /// <typeparam name="U"></typeparam> /// <param name="item"></param> /// <param name="rangeStart"></param> /// <param name="rangeStop"></param> /// <returns></returns> public static bool IsInRange<U> (U item, U rangeStart, U rangeStop) where U : IComparable<U> { if (item == null || rangeStart == null || rangeStop == null) return false; return rangeStart.CompareTo (item) >= 0 && rangeStop.CompareTo (item) <= 0; } /// <summary> /// /// </summary> /// <param name="number"></param> /// <param name="rangeStart"></param> /// <param name="rangeStop"></param> /// <returns></returns> public static bool IsInRange (double number, double rangeStart, double rangeStop) { return number >= rangeStart && number <= rangeStop; } /// <summary> /// /// </summary> /// <param name="number"></param> /// <param name="rangeStart"></param> /// <param name="rangeStop"></param> /// <returns></returns> public static bool IsInRange (float number, float rangeStart, float rangeStop) { return number >= rangeStart && number <= rangeStop; } /// <summary> /// Check if a given number is greater then or equal and less then or equal to a range. /// x >= y && x <= z /// </summary> /// <param name="number"></param> /// <param name="rangeStart"></param> /// <param name="rangeStop"></param> /// <returns></returns> public static bool IsInRange (int number, int rangeStart, int rangeStop) { return number >= rangeStart && number <= rangeStop; } }