1

I am quite new to C# and I was wondering if there is a Class or a data structure or the best way to handle the following requirement... I need to handle a COUPLE of int that represent a range of data (eg. 1 - 10 or 5-245) and I need a method to verify if an Int value is contained in the range... I believe that in C# there is a class built in the framework to handle my requirement... what I need to do is to verify if an INT (eg. 5) is contained in the range of values Eg (1-10) ...

in the case that I should discover that there is not a class to handle it, I was thinking to go with a Struct that contain the 2 numbers and make my own Contain method to test if 5 is contained in the range 1-10)

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
yamini
  • 41
  • 3
  • 3
    What is your use case? How do you want to verify that the number is in range? What do you want to happen if it is not? – Gabe Jan 11 '12 at 22:27

6 Answers6

4

in the case that I should discover that there is not a class to handle it, I was thinking to go with a Struct that contain the 2 numbers and make my own Contain method to test if 5 is contained in the range 1-10)

That's actually a great idea as there's no built-in class for your scenario in the BCL.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Actually I have just found Enumerable.Range(1,100).Contains(x) but I am not sure about the performances... is it better a list/hash-table with several millions of Enumerable.Range(1,100) or a list/hashtable with several millions of struct and a simpler check method??? http://stackoverflow.com/questions/3188672/how-to-elegantly-check-if-a-number-is-within-a-range – yamini Jan 11 '12 at 22:35
  • 1
    @yamini, yeah, great, but this is a method, not a data structure as you were asking. – Darin Dimitrov Jan 11 '12 at 22:38
2

Nothing exists that meets your requirements exactly.

Assuming I understood you correctly, the class is pretty simple to write.

class Range
{
    public int Low {get; set;}
    public int High {get; set;}

    public bool InRange(int val) { return val >= Low && val <= High; }
}

A Tuple<int,int> would get you part of the way but you'd have to add an extension method to get the extra behavior. The downside is that the lower- and upper-bounds are implicitly Item1 and Item2 which could be confusing.

// written off-the-cuff, may not compile
public static class TupleExtension
{
    public static bool InRange(Tuple<int, int> this, int queryFor)
    {
         return this.Item1 >= queryFor && this.Item2 <= queryFor;
    }
}
Austin Salonen
  • 49,173
  • 15
  • 109
  • 139
2

You're looking for a range type; the .Net framework does not include one.

You should make an immutable (!) Int32Range struct, as you suggested.
You may want to implement IEnumerable<int> to allow users to easily loop through the numbers in the range.

You need to decide whether each bound should be inclusive or exclusive.
[Start, End) is probably the most obvious choice.
Whatever you choose, you should document it clearly in the XML comments.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
1

You could create an extension if you want to avoid making a new type:

public static class Extensions
{
  public static bool IsInRange(this int value, int min, int max)
  {
    return value >= min && value <= max;
  }
}

Then you could do something like:

if(!value.IsInRange(5, 545))
  throw new Exception("Value is out of range.");
itsme86
  • 19,266
  • 4
  • 41
  • 57
0

i think you can do that with an array. some nice examples and explanation can be found here: http://www.dotnetperls.com/int-array

Dante1986
  • 58,291
  • 13
  • 39
  • 54
0

Nothing built in AFAIK, but (depending on the size of the range) an Enumerable.Range would work (but be less than optimal, as you're really storing every value in the range, not just the endpoints). It does allow you to use the LINQ methods (including Enumerable.Contains), though - which may come in handy.

const int START = 5;
const int END = 245;

var r = Enumerable.Range(START, (END - START)); // 2nd param is # of integers
return r.Contains(100);

Personally, I'd probably go ahead and write the class, since it's fairly simple (and you can always expose an IEnumerable<int> iterator via Enumerable.Range if you want to do LINQ over it)

Mark Brackett
  • 84,552
  • 17
  • 108
  • 152