Check integer overflow in C#
Forums:
//no range check by default
byte b1 = 200;
byte b2 = 100;
byte b3 = (byte)(b1 + b2);
//b3 is 44 (300 - 256)
checked {
byte b4 = (byte)(b1 + b2); //<-Error
}
unchecked {
byte b5 = (byte)(b1 + b2);
//b5 is 44
}
- Log in to post comments