DEV Community

Ahmet Burhan Simsek
Ahmet Burhan Simsek

Posted on

Understanding and Using the ‘Unchecked’ Keyword in C#

Understanding and Using the ‘Unchecked’ Keyword in C

The “unchecked” keyword in C# is used to turn off overflow checking for integer operations within a block of code. This indicates that the program will not raise an exception and will instead carry on with the resulting value if an operation might cause an overflow or underflow.

Performance concerns and dealing with existing code that utilizes integer arithmetic when you know that overflow or underflow won’t happen are the main reasons to use the “unchecked” keyword. But if overflow or underflow does happen, it can result in unexpected behavior and issues, therefore it should be used with caution.

If an operation would produce an overflow or underflow (i.e: a value that is too large or small to be represented by the data type being used), the program would not raise an exception and would instead continue to run with the resulting value. This is the default behavior for integer operations in C#. This value is the outcome of truncation or wrap-around.

For instance, if we have the code below;

int x = int.MaxValue; 
x = x + 1;
Enter fullscreen mode Exit fullscreen mode

Here the value of x will be -2147483648 instead of raising an exception.

Screenshot for the values in runtime for the code written above

To overcome this, C# provides the “checked” keyword, which can be used to specify that an operation or block of code should raise an exception if an overflow or underflow occurs.

int x = int.MaxValue; 
checked 
{ 
  x = x + 1; 
}
Enter fullscreen mode Exit fullscreen mode

This will raise an exception of type System.OverflowException.

Screenshot for the System.OverflowException which has been raised for the code above

It’s important to note that you may explicitly turn off overflow checking within a block of code by using the “unchecked” keyword.

Screehshot for the unchecked version of the code written above

In general, unless you have a special reason to anticipate and handle these circumstances, it’s advised to utilize “checked” blocks of code to prevent unexpected behavior and defects caused by integer overflow or underflow.

Let’s check it with another example;

Screenshot for another example for the “Unchecked” keyword usecase

Console.WriteLine(unchecked(long.MaxValue + long.MaxValue));
Console.WriteLine(unchecked((ulong)(long.MaxValue + long.MaxValue)));
Console.WriteLine(ulong.MaxValue);
Enter fullscreen mode Exit fullscreen mode

The provided code is a C# program that publishes the results to the console after performing some arithmetic operations on the long and ulong data types’ maximum values using the “unchecked” keyword.

To explain the code written above;

In the first line; 
long.MaxValue + long.MaxValue 
exceeds the maximum value that a long variable can represent, but 
because of the unchecked keyword, the program won't throw an exception 
if the result of the addition exceeds the maximum value that a long 
variable can represent. Instead, a wrap-around will provide the outcome, 
which in this case is -2.

In the second line;
long.MaxValue + long.MaxValue is exceeding the maximum value that can 
be represented by a ulong variable, and then converting it to a ulong 
variable. But because of the unchecked keyword, the program will not 
raise an exception if the result of the addition exceeds the maximum 
value that can be represented by a ulong variable. Instead, the result 
will be the result of a wrap-around, which is 18446744073709551614 in 
this case.

In the third line;
it just prints the maximum value that a ulong variable can store, 
which is 18446744073709551615.
Enter fullscreen mode Exit fullscreen mode

TL;DR

When using integer operations within a block of code in C#, overflow checking is turned off with the “unchecked” keyword.

What is overflow in C# ?

When an arithmetic operation returns a number that is either too large or too little to be represented by the data type being used, the condition is referred to as an overflow.

For instance;
if a variable is of type int and its maximum value is 2,147,483,647 and you add 1 to it,
it will cause an overflow because the result will be 2,147,483,648 which is too large to be represented by an int variable.

There is also underflow in C#.

What is underflow in C# ?

Similar to overflow, underflow happens when the operation’s result is too little to be represented by the data type.

When you remove 1 from an int variable whose minimum value is -2,147,483,648
the result will be -2,147,483,649
which is too tiny to be represented by an int variable and will result in an underflow.

How C# handles overflow and underflow in runtime ?

The C# language has a technique called as “wrap-around” to manage overflow and underflow in both situations. This implies that the program will carry on with the output value in the event of an overflow or underflow. It is advised to use the “checked” keyword to make sure that overflow and underflow are handled correctly and to prevent unexpected behavior because doing otherwise can result in problems and unexpected behavior.

I appreciate you taking the time to read the answers above and learn more about the “unchecked” and “checked” keyword in C#.
Programming in C# involves understanding how this keyword functions and when and why to use it, which may lead to more effective and reliable code.
Please don’t hesitate to reach me out if you have any further questions or if something is not obvious.

I want to thank you once again for reading and I hope you found the material to be useful 🤗

Top comments (0)