Checked Keyword in C# 4.0

This article is all about understanding the “checked” keyword in C# 4.0. This keyword is used for raising an exception whenever any integral / numeric variable overflow its max value. Normally if we try to add a value to a variable that is already holding its datatype max value, then that variable value will be set to a value that is the minimum value of that datatype (or rather we can say in terms that the value would be invalid state) without raising any exception by the compiler. This is because there are some compiler level settings that are turned off by default. Although you could manually turn on this setting, if in any case you don't need to turn it on then you have the alternative of using the “Checked” keyword. In this article we'll be focusing on both the concept of “Turning on the Compiler level settings” and “Using the Checked Keyword”.

For example If the byte datatype ranges between 0 and 255. If the user declares a variable of byte type, then normally that variable could hold a value between 0 and 255 because of its Type declared as “byte”. If the user now adds a constant value to this variable that is already holding the maximum value of its type then the value of this variable would be in an inconsistent state. Let's look at an example.

  1. static void Main(string[] args)  
  2.         {  
  3.             try  
  4.             {  
  5.                 byte byteA = byte.MaxValue;  
  6.                 Console.WriteLine("Value of byteA before increment {0}", byteA);  
  7.                 byteA = ++byteA;  
  8.                 Console.WriteLine("Value of byteA after increment {0}", byteA);  
  9.             }  
  10.             catch (Exception ex)  
  11.             {  
  12.                 Console.WriteLine(ex.Message);  
  13.             }  
  14.             Console.ReadLine();  
  15.         }  
The following is the output for it.

check demo

As it can be seen that the effect of adding / incrementing the variable value (that is already holding the type max value) by 1 forces the compiler not to raise an exception. Instead recalculate the value and set it to its min value as per datatype that is an invalid value (since the compiler level settings are by default turned off). So how do we notify the user about the exception? To resolve this issue we can implement it in one of the following two ways:

 

  1. Using the Compiler Setting Option.

  2. Using the Checked Block.

1. Using the Compiler Setting Option: To do this we just need to enable the compiler setting within our application. Just right-click on your project then select project properties.

  1. Click on the Build option from the properties menu.

    build option

  2. Click on the Advanced Button.

    advance build option

  3. Check the checkbox “Check For arithmetic overflow/underflow”.

    build setting

  4. Just save the settings and try running the application.

    Your output will throw an exception now.

    The following is the snapshot for it:

    exception

2. Using Checked Block: The same thing could be implemented using the checked block also. Just placing the code inside the checked block will help us resolve this issue. The following is the block of code using a checked block.

  1. static void Main(string[] args)  
  2.         {  
  3.             try  
  4.             {  
  5.                 checked  
  6.                 {  
  7.                     byte byteA = byte.MaxValue;  
  8.                     Console.WriteLine("Value of byteA before increment {0}", byteA);  
  9.                     byteA = ++byteA;  
  10.                     Console.WriteLine("Value of byteA after increment {0}", byteA);  
  11.                 }  
  12.             }  
  13.             catch (Exception ex)  
  14.             {  
  15.                 Console.WriteLine(ex.Message);  
  16.             }  
  17.             Console.ReadLine();  
  18.         }  
The following will be the output for it.

arithmetic operation resulted

 

Up Next
    Ebook Download
    View all
    Learn
    View all