checked & unchecked keyword in C#

In this article, we will discuss about the checked and unchecked keywords. Let’s start with their definitions first. As per MSDN checked means,

The checked keyword is used to explicitly enable overflow checking for integral-type arithmetic operations and conversions.

and unchecked means,

The unchecked keyword is used to suppress overflow-checking for integral-type arithmetic operations and conversions.

Here, overflow checking means that when the value of any integral-type exceeds its range, it does not raises any exception, instead will give unexpected results. Let’s discuss with an example of integers.

We know that range on an integer is 2147483647. Let’s see what happens when we try to add more to it, say add 50 to itSee the code below:

checked-unchecked in C#

It gives the compile time error :

Error 3 The operation overflows at compile time in checked mode ...\checked\Program.cs 23 29 checked.

So, at-least directly we cannot do this. Also, in most of our cases, we work with values from database. So we fetch them, store in variables and then perform any operations on it. In such a case, there will be no compile time or run time error, instead will be unexpected results. Let’s check see what happens:

checked-unchecked in C#

As we discussed, an unexpected result. The unexpected result is generated in a pattern. Rion Williams provided a great explanation here. The add operation adds a given number up-to maximum possible value of its range. After that it resets itself to its minimum value of its range and then starts adding again. In our case, we were adding 50 to 2147483647. So let’s see how it works.

  • It starts adding 1 at a time. By first addition of 1, it resets itself to minimum value of -2147483648. Now further 49 is to be added.
  • Then it re-starts from its minimum value of its range, which is -2147483648 and starts adding them one by one.

-2147483648 + 49 = -2147483599

We can handle this overflow of the value using the checked keyword. It will not give correct result but at-least it will give you exception of the type System.OverflowException and you can easily identify the issue. So apply the checked keyword, wrap the code in try-catch block and perform the alternate operation in catch block. Let’s change the code accordingly now:

checked-unchecked in C#

So you can now at-least handle such kind of situation, instead of giving incorrect results to the user.

Now, earlier in our discussion, we discussed that directly we cannot add more value to the maximum value of integer. What if we need to do this. This where we can use the unchecked keyword. Just use the same code with the unchecked keyword and it will compile and will even run to give the results. See the code below:

checked-unchecked in C#

So to summarize the discussion:

  • If we are directly trying to add something to maximum value of integer, we will get compile time error.
  • To avoid the above compile time error, we can use the unchecked keyword.
  • Using the unchecked keyword or indirectly adding more values to a maximum value of its range, results in unexpected results, generated in a pattern.
  • To handle the overflow value, we can use the checked keyword, apply try-catch block and handle the operation in catch block.
  • These keywords  can be applied as blocks of code or directly on the variables(like in our discussion above). See the code below:

checked-unchecked in C#

So this about the use of checked-unchecked keywords. Hope you enjoyed reading it…!!!


About Jasminder

.Net developer and blogger by profession, keen to learn new technologies, love nature, music and cricket.
This entry was posted in C#. Bookmark the permalink.

I have a suggestion..

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s