Saturday, December 6, 2014

checked & unchecked keywords 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:

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:

As we discussed, an unexpected result. The unexpected result is generated in a pattern. 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:

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:

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:

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

No comments:

Post a Comment