C# Basics: What Checked and Unchecked Operations Are

You may have come across the following questions:

  • What are checked and unchecked primitive operations?
  • How to handle overflows in C#?
  • Does the CLR handle overflow for developers?

So let us start with understanding a program. Consider the code listed below:

  1. static void Main(string[] args)  
  2. {  
  3.   
  4.    Byte b = 100;  
  5.    b = (Byte) (b + 200);  
  6.    Console.WriteLine(b);  
  7.   
  8.    Console.ReadKey(true);  
  9. }  
On running you will get 44 as the output. There is one serious observation about the preceding output; Byte is a primitive type that is unsigned an 8 bit value. Even though the preceding add operation results an overflow, the CLR does not complain about that.

By default, the CLR does not throw an overflow exception.

However there may be a requirement in which, you may need to throw the overflow exception. The CLR gives us the option to configure whether an overflow exception is thrown or not.

We can configure this at any of the following three levels:  
  1. Statements
  2. Operations
  3. Application

Application level

You can configure to throw an overflow exception at the project level. To configure, right-click on the project, select Properties and then build and then advanced. In the advanced build setting check the checkbox- check for arithmetic overflow/underflow.

check for arithmetic overflow

After configuring the check for arithmetic overflow/underflow at the project level, if you then and run the application, the CLR will throw an exception as in the following:

arithmetic overflow

We are getting the overflow exception since we have configured it at the project level to throw an exception on arithmetic overflow/underflow.

Statements level

You may not want to throw the exception for all the operations in the project. So you can also configure whether to throw an overflow/underflow exception or not at the statement or expression level.

C# provides us checked and unchecked operator and statement to configure overflow/underflow exception at the operation and statement level respectively.

Using the check statement, you can throw an overflow exception in the preceding code as listed below:

  1. static void Main(string[] args)  
  2. {  
  3. checked  
  4. {  
  5.   
  6.    Byte b = 100;  
  7.    b = (Byte)(b + 200);  
  8.    Console.WriteLine(b);  
  9. }  
  10.   
  11.   
  12.    Console.ReadKey(true);  
  13. }  
By using the check statement, we can ask the CLR to throw an overflow or underflow exception caused by the arithmetic operations.

Operations level

C# also allows us to apply a cheked/unchecked operator on a single expression. The preceding code can be written as listed next:
  1. static void Main(string[] args)  
  2. {  
  3.    Byte b = 100;  
  4.    b = checked ((Byte)(b + 200));  
  5.    Console.WriteLine(b);  
  6.    Console.ReadKey(true);  
  7. }  
Understanding Internal

The CLR has two instructions for each arithmetic operation.  
  • Addition: add and add.ovf
  • Subtraction: sub and sub.ovf
  • Multiplication: mul and mul.ovf
  • Conversion: conv and conv.ovf

As the instruction name suggests, an add instruction does not throw an overflow exception whereas add.ovf throws an overflow exception. When we do an add operation then by default the CLR executes an add IL instruction. However when we do a checked add operation, the CLR executes an add.ovf exception that will throw an overflow exception.

Summary

There were three questions in the beginning, let us see them one by one.

  1. What are checked and unchecked primitive operations?

    Checked and unchecked operations check for the overflow in the arithmetic operations. If checked then the operation may throw the overflow/underflow exception. If unchecked then it will not throw the overflow exception.

  2. How to handle overflow in C#?

    Using the checked and unchecked an overflow can be handled in the C#.

  3. Does CLR handle overflow for developers?

    By default the CLR does not throw an overflow exception. However using checked and unchecked developers can configure whether to throw or not.

Up Next
    Ebook Download
    View all
    Learn
    View all