Integers in .NET (or C #)

Am I blind or does the .NET Framework not provide any integer range class? That is, a type that will prevent the setting of a value beyond certain boundaries that are not the full range of the underlying data type. For example, an integer type that would limit its values ​​from 1 to 100. Showing my age here, but back in '93, I remember that I used such things in Modula-2 (eeek!), But I did not see explicit support for the / language for him ever since.

Am I just missing something, or is it the case β€œis it so simple to make your own that the framework does not bother”?

Greetings.

+3
source share
3 answers

- 1) - . .


1) , enum, .

+1

, , . # 3.5 Spe#.

# 4.0, CodeContract (, , BCL).

+2

, , , , .

. , :

private int myRangedIntValue;
public int MyRangedIntValue
{
    get { return myRangedIntValue; }
    set 
    { 
        myRangedIntValue = Math.Max(1, Math.Min(100, value));
    }
}

Custom type benefits are diminished. If you leave the standard int types, you will have much fewer types that you need to worry about compiling, data binding, etc.

+1
source

Source: https://habr.com/ru/post/1735962/


All Articles