What is arithmetic overflow?
I will try to explain this concept by using an example.
For example, we have a byte variable i.e., it can hold a value from 0 to 255. If we try to assign the value greater than 255, it will get overflowed.
Example
- byte a = 160;
- byte b = 100;
- byte c = (byte)(a+b);
Why four?
The binary value for 260 is 100000100 (9 bits) but byte can hold a max of 255 (i.e max 8 bits) so the left most digit will get truncated and the value will be 00000100 which is equivalent to 4 in decimal form.
Arithmetic overflow can cause unexpected results and can break your application. In the above example, the expected result is 260 but the actual result is 4 which is unexpected.
From the above example, it can be easily concluded that we must select the data type based on what can be the maximum value for that variable.
How Are Arithmetic Overflows Handled in C#?
With the default setting, C# compiler will not throw an exception if arithmetic overflow happens.
Example
- class Program
- {
- static void Main(string[] args)
- {
- byte a = 160;
- byte b = 100;
- byte c = ((byte)(a+b));
- }
- }
For every arithmetic operation, there are two overload methods.
Examples:
For Addition
Examples:
For Addition
- add - will not throw an exception if overflow happens
- add.ovf- will throw an exception if overflow happens
For subtraction
- sub - will not throw an exception if overflow happens
- sub.ovf- will throw an exception if overflow happens
And the short answer to this is using the checked and unchecked keywords.
If we want the code to throw an exception when arithmetic overflow happens we need to use checked keyword.
Example
- class Program
- {
- tatic void Main(string[] args)
- {
- byte a = 160;
- byte b = 100;
- byte c = checked((byte)(a+b));
- }
- }
[System.OverflowException] {"Arithmetic operation resulted in an overflow."}
With default settings there is no need to use unchecked keyword but if we set "check for arithmetic overflow/underflow" to true in build setting then the below code will throw an overflow exception:
- class Program
- {
- static void Main(string[] args)
- {
- byte a = 160;
- byte b = 100;
- byte c = (byte)(a+b);
- }
- }
[System.OverflowException] {"Arithmetic operation resulted in an overflow."}
In such cases if you want particular operations to ignore arithmetic overflow we can use unchecked keyword as,
- class Program
- {
- static void Main(string[] args)
- {
- byte a = 160;
- byte b = 100;
- byte c = unchecked((byte)(a+b));
- }
- }
Note
Enabling "check for arithmetic overflow/underflow" to true in build setting can cause a performance issue as in every operation it will check for arithmetic overflow/underflow
Enabling "check for arithmetic overflow/underflow" to true in build setting can cause a performance issue as in every operation it will check for arithmetic overflow/underflow

Viknaraj ManogararajahPosted Jul 19, 2018, 6:50 AM
Nice Article...........