Is there a succinct way to represent a bounded numeric value in .NET 3.5?
By this I mean a value such as a percentage (0 – 100) probability (0 – 1) or stock level (0 or above).
I would want a ArgumentOutOfRangeException (or equivalent) to be thrown if an out-of-range assignment was attempted. I would also want static MaxValue and MinValue properties to be available.
It was suggested in a comment to another SO question that this was a good use of a struct.
I don’t think there is a built-in way like the Ada ‘range’ keyword, but you could easily create a type like RestrictedRange<T> that had a min and max value along with the current value:
Since there is an implicit conversion to automatically get the value, this will work:
In addition, you can do things like this