C# Professional - Numbers

talent-agile
1,838 views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Arithmetic Overflow

Overflow is an operation that occurs when a calculation produces a result that is greater in magnitude than that which a given register or storage location can store or represent.

See this example of overflow:

1
9
10
11
12
13
14
// {...}
var intValue = Int32.MaxValue;
var overflow = intValue + 1;
Console.WriteLine($"intValue: {intValue}");
Console.WriteLine($"overflow: {overflow}");
// {...}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Detecting & Catching Overflows

As you can see, when reaching the maximum value of a given numeric type, trying to add anything will overflow the capacity and result in a wrong value.

By default, the .Net runtime will not raise exceptions when doing numeric calculations. If you want to ensure that arithmetic operations will throw overflow exceptions if an overflow happens, you need to use the checked { ... } code block.

When using the checked { ... } code block, if any arithmetic operation causes an overflow, an OverflowException will be thrown, and will need to be catched and handled.

Note: Depending on the build options of your project, detection of overflow and underflow can be automatically enabled, removing the need for the checked { ... } code block. The option can be activated in the "Advanced" build settings of any C# project.

Overflow / Underflow build option

1
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// {...}
checked
{
try
{
var intValue = Int32.MaxValue;
var overflow = intValue + 1;
Console.WriteLine($"intValue: {intValue}");
Console.WriteLine($"overflow: {overflow}");
}
catch (OverflowException)
{
Console.WriteLine("Overflow !");
}
}
// {...}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Note: Overflows can also happen when the result is smaller than the smallest value that can be stored or represented. These are often called underflows. The result of the operation when not using the checked { ... } code block is zero.

Open Source Your Knowledge: become a Contributor and help others learn. Create New Content