C # by default, if the constant expression value exceeds the maximum value of the target type, compilation errors will occur.
If the target data type cannot contain data of a non-numeric expression, the data will be truncated when the value is assigned.
CopyCodeThe Code is as follows: Class Program
{
Static void main (string [] ARGs)
{
Int n = int. maxvalue; // n = 2147483647
N = n + 1;
System. Console. writeline (N );
}
}
In this case, the memory storage data can be used to explain the preceding Int. the value of maxvalue is in the memory of 32-bit 1 and changes to 32-Bit 0 after the auto-increment is 1. In this case, 0 is regarded as a negative number, so-2147483648 is obtained.
Placing the above Code in the checked block will lead to the system. overflowexception type.
Copy codeThe Code is as follows: Class Program
{
Static void main (string [] ARGs)
{
Checked
{
Int M = int. maxvalue;
M = m + 1;
System. Console. writeline (m );
}
}
}
C # is placed in the checked block. If an overflow value is assigned during running, an exception is thrown.
UncheckedUsed to cancel overflow checks for integer arithmetic operations and conversions.