locked
"between" in C# RRS feed

  • Question

  • Is there a way to write a "between" in C#? instead of the following

    if (MyValue <= UpperLimit && MyValue >= LowerLimit)

    or is there a better and less verbose way of writing the above if statement?

    • Edited by bsobaid Thursday, September 24, 2009 4:47 PM
    Thursday, September 24, 2009 4:15 PM

Answers

  • It could even be generic:
    public static bool Between<T>(this T value, T lowerLimit, T upperLimit) where T : IComparable<T>
    {
        {
            return value.CompareTo(lowerLimit) >= 0 && value.CompareTo(upperLimit) <= 0;
        }
    }
    • Proposed as answer by Wole Ogunremi Thursday, September 24, 2009 4:32 PM
    • Marked as answer by nobugzModerator Thursday, September 24, 2009 11:29 PM
    Thursday, September 24, 2009 4:31 PM
  • I think I've been looking at code for so long now, it reads quite naturally using the normal syntax.

    Although I personally order the test so the the lower limit is on the left, the test value is in the centre, and the higher limit is on the right, like so:

    if (lower <= value && value <= higher)
    {
        ...
    }

    Thursday, September 24, 2009 10:59 PM

All replies

  • I would recommend rewriting it as (I think it's easier to put lower first, plus actually make it work (you're checking against upper, so it'll always be true in yours...):

        if (MyValue >= LowerLimit && MyValue <= UpperLimit)

    There isn't really a shorter way to do that.  You could always make an extension method:

        public static boolean Between(this double value, double upper, double lower)
        {
            return (value >= lower && value <= upper);
        }

    In which case, you could do:

        if (MyValue.Between(LowerLimit,UpperLimit))
        { ... }


    Reed Copsey, Jr. - http://reedcopsey.com
    • Proposed as answer by Wole Ogunremi Thursday, September 24, 2009 4:31 PM
    Thursday, September 24, 2009 4:28 PM
    Moderator
  • It could even be generic:
    public static bool Between<T>(this T value, T lowerLimit, T upperLimit) where T : IComparable<T>
    {
        {
            return value.CompareTo(lowerLimit) >= 0 && value.CompareTo(upperLimit) <= 0;
        }
    }
    • Proposed as answer by Wole Ogunremi Thursday, September 24, 2009 4:32 PM
    • Marked as answer by nobugzModerator Thursday, September 24, 2009 11:29 PM
    Thursday, September 24, 2009 4:31 PM
  • I think I've been looking at code for so long now, it reads quite naturally using the normal syntax.

    Although I personally order the test so the the lower limit is on the left, the test value is in the centre, and the higher limit is on the right, like so:

    if (lower <= value && value <= higher)
    {
        ...
    }

    Thursday, September 24, 2009 10:59 PM
  • A related question. If there is a couple of ANDs in an if statement does the compiler move on if the first AND is false or it checks all of them anyways?? Same question for OR. If first OR is true does it still check the rest?
    Friday, September 25, 2009 1:20 PM
  • A related question. If there is a couple of ANDs in an if statement does the compiler move on if the first AND is false or it checks all of them anyways?? Same question for OR. If first OR is true does it still check the rest?
    The compiler generates code that will do boolean short-circuiting. So if the first item in a compound AND is false, it won't compute the remaining ones. And if the first item of a compound OR is true, it won't compute the remaining ones.
    Friday, September 25, 2009 1:49 PM