![IComparable](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhmahvqDyZWN50BOJkzVK4zrlEgbvmE8WOsg3KhCygY01rroCmjlGqjI0mM7DzEOJK3VgB5wJV-FBHA2mxYwFYHI9N7_AEhglfKTj282jWRsfUiCkocyDvQf01iqaFW3Bx8bZyn45EmcGU/s288/applesandoranges.jpg)
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