Working with Checked and Unchecked operators in C# Step by Step

 

This article will hover around "checked" and "unchecked" operators in C# step by step. We will first discuss the scenario where these keywords can be used. "Checked" keyword us helps to check if the destination datatype is overflow in an arithmetic calculation and also it will check block of line code.

For that first we should know the maximum value datatype "int" can hold. To know that following is the code snippet which will show output on console prompt of maximum value of "int" datatype. Output shown on console prompt "int" maximum value can hold is "2147483647".

On Visual Studio if you load datatype with value more than its capacity in such scenario datatype overflow occurs. It is sometime left undetected by compiler and its compilation is done with unexpected (-2)result. If we see value of variable "a" and "b" are so huge that it can never result into "-2". So how to overcome such issue in C# program.

Here program executed easily compiler without showing any error indication resulting into abnormal result. It would be really helpful some sort of exception is shown before displaying result.

Checked Operator

Now let us run the above application using "checked" keyword and see how application behaves.

With the use of "checked" keyword complier will check data before sending from right side of arithmetic expression to left side.

If more data is send from right to left side of expression then it will show an overflow exception message as shown below and result will not be displayed.

With the use of "checked" keyword expression is not compiled without checking by compiler. And with itís use it is ensured in such scenario where we do not want datatype overflow to occurred.

If you wish the same to be handled with error showing on console prompt rather than directly displaying message window on visual studio. Use try and catch code as shown in the image down below and get proper error message displayed so that can be easily understood by the user.

Unchecked Operator

"Unchecked" keyword be have ALMOST the same way as default, but is useful to bypass datatype checks especially the constant.

Consider the below scenario where have datatype "int a" and "int b" which are constant and if we put addition expression and try to push it result value in "int c" as shown in the image down below then it shows a red underline under the expression and now if mouse is hovered over the red under line then it will show overflow message.

This is the scenario where default check of overflow is done by compiler even before program is executed.

Now suppose if you want to skip this overflow check and want to execute the program then that can be done with the use of "unchecked" keyword. Following is the image snapshot of the program where we have used "unchecked" keyword.

With the use of "unchecked" keyword red underline overflow exception error which was earlier detected by compiler has been removed. As of addition expression is going without getting checked by the compiler.

So with the use of "checked" keyword compiler will check by ensuring no overflow exception is occurred and with use of "unchecked" keyword compiler skips the expression check by moving ahead.

Expecting that this article is helpful to reader in understanding "checked" and "unchecked" keyword in C#.

Following is the fresher's 100 hours project series video covering topic end to end. Do view this first video followed by other videos from this series to learn .NET practically: -

 

 

:: SHARE THIS POST ::

        
 

More C# Articles To Read Next:

 
Dec
26
2016

Remember Top 3 important points of out and ref in C#

CLICK TO READ FULL ARTICLE

Views - 191 |Category - C#

Dec
07
2016

Type Safe in C#.NET step by step using example

CLICK TO READ FULL ARTICLE

Views - 211 |Category - C#

Dec
06
2016

App Domain in C#.NET Step by Step using example

CLICK TO READ FULL ARTICLE

Views - 365 |Category - C#

Dec
01
2016

Delay signing in C# with example

CLICK TO READ FULL ARTICLE

Views - 193 |Category - C#

Nov
28
2016

Tuples in c# example step by step

CLICK TO READ FULL ARTICLE

Views - 352 |Category - C#

 
Author: Gurunatha Dogi
is a software engineer by profession and founder of Onlinebuff.com, Onlinebuff is a tech blog which covers topics on .NET Fundamentals, Csharp, Asp.Net, PHP, MYSQL, SQL Server, Cell Phones and lots more. Follow me @ and twitter.
Leave a Comment......

All fields marked with * are mandatory

eesi5