Like T-SQL, the between extension method is inclusive and the start parameter doesn't have to be lower than the end parameter.
Here's what some test cases look like:
5.Between(1, 10) // true 5.Between(10, 1) // true 5.Between(10, 6) // false 5.Between(5, 5)) // true
Here's the method:
public static bool Between<T>(this T target, T start, T end) where T : IComparable { if (start.CompareTo(end) == 1) return (target.CompareTo(end) >= 0) && (target.CompareTo(start) <= 0); return (target.CompareTo(start) >= 0) && (target.CompareTo(end) <= 0); }
No comments:
Post a Comment