Overflow check in C # works using (or not use) overflow check in CIL.
For example, consider C # code:
public static int AddInts(int x, int y)
{
return x + y;
}
Without overflow checking, it will compile something like this:
.method public hidebysig static int32 AddInts(int32 x, int32 y) cil managed
{
.maxstack 2
IL_0000: ldarg.0
IL_0001: ldarg.1
IL_0002: add
IL_0003: ret
}
With an overflow check, it will compile something like this:
.method public hidebysig static int32 AddInts(int32 x, int32 y) cil managed
{
.maxstack 2
IL_0000: ldarg.0
IL_0001: ldarg.1
IL_0002: add.ovf
IL_0003: ret
}
, CIL add, , checked unchecked #. , , , , #, , .
CIL, , , , . ( , .NET ), , jo x86.
, " ".
, unchecked , checked, , , . , , , - , . * , , , , .
unchecked.
, , .
32- () unchecked(x + y) " x y 32- ", checked(x + y) " x y 32- , ".
, unchecked(int.MaxValue + int.MaxValue) -2, , checked(int.MaxValue + int.MaxValue) , , .
, , -2.
, " , ?"
- , , , :
unchecked. - , , - , , :
checked. - , , - :
long BigInteger, int, , (, int, , ). - , , , , " " ":
checked, unchecked , .
, , . , ( ?), , checked, , unchecked.
, ; "-", - .
- , " ", int , "" , .
, ", " - ; OverflowException, , , , , , , , . , , , , , .
:
- , (-,
unchecked) unchecked, . - ,
OverflowException, - () checked. unchecked , checked , , . ( , , ).
* , , , , , . , , , .