Just to point out a caveat that could bite you on the backside if you're not
careful.
Technically, 'between' does NOT include either of the limits. I.E., 4 and 5
are between 3 and 6 but 3 is NOT between 3 and 6.
However, in everyday useage, between is often considered to include either
of the limits. I.E., 3, 4, 5, and 6 are ALL between 3 and 6. For example,
This is how the between operator is implemented in TRANSACT-SQL
The point is, that to be able to use any 'between' function/operator with
confidence you must be aware of just what that function/operator considers
'between' to mean.
"DBC User" <db*****@gmail.comwrote in message
news:11**********************@o38g2000hse.googlegr oups.com...
On Nov 5, 9:56 am, "Larry Smith" <no_spam@_nospam.comwrote:
This is a very simple question, I have a number and I want to check if
the number is between 2 integers. I can use 'if' to resolve this
situation. But I am curious, if there is any other verbs that I can
use in C# like sql 'between'?
No but you can always roll your own function. How about something like
this::
public static bool IsInRange<T>(T value, T minValue, T maxValue) where T
: IComparable<T>
{
Debug.Assert(minValue.CompareTo(maxValue) <= 0);
if (minValue.CompareTo(maxValue) 0)
{
string message = string.Format("\"minValue\" ({0}) must be less
than or equal to \"maxValue\" ({1})",
minValue.ToString(),
maxValue.ToString());
throw new ArgumentException(message);
}
return (value.CompareTo(minValue) >= 0 && value.CompareTo(maxValue)
<= 0);
}
bool excellentStudent = IsInRange(grade, 90, 100);
This is a pretty cool stuff.
Thanks.