I have a list of non-overlaping ranges (ranges of numbers, e.g. 500-1000, 1001-1200 .. etc), is there an elegant and fast way to do lookup by only passing a number? I could use List.BinarySearch() or Array.BinarySearch() but I have to pass the type of the range object (Array.BinarySearch(T[], T)), I can pass a dummy range object and get the job done (only do the comparison with the range start) but I was wondering if this can be done in a cleaner way by only passing an integer and getting the range object, is there a way to achieve this?
I have a list of non-overlaping ranges (ranges of numbers, e.g. 500-1000, 1001-1200 ..
Share
Three options:
The third option would go something like:
Apologies if I’ve got any off-by-one errors. I haven’t tested it at all, but it does at least compile 🙂