Checked: -The checked keyword is used to explicitly enable overflow
checking for integral-type arithmetic operations and conversions.
Unchecked: - The unchecked keyword is used to suppress overflow-checking
for integral-type arithmetic operations and conversions.
Now, let’s create a small console application and try to demonstrate the use of
both the keywords (checked and unchecked).
Now, for demonstration purpose let’s take “int” data type and see what
the maximum value it can hold.
Add the below code snippet in to your application to known the maximum value can
be hold by the int data type.
The below diagram shows the exact value can be hold by “int” data type.
Now, let’s see where the checked keyword can help us in making your code more
useful.
In the above code snippet you can see that I have tried to add extra value to the “z” variable which is of “int” data type and it can hold only “2147483647” value, now let’s see what happen when we try to do that.
This is somewhat we were not expecting your expectation was the compiler should
throw some error (overflow) or exception.
Checked keyword helps us to achieve this like below code snippet.
Now, when you run your application the compiler will raise an error like below diagram.
In simple words the checked keyword is used in scenarios where you want to
ensure that your left hand side data type is not getting overflow.
Now, let’s see what the use of unchecked keyword is.
Note: In simple words the unchecked keyword behaves almost the same way as the
default behavior of the compiler.
Let’s prove that the above point by a simple demonstration.
In above code snippet you can see that I have just added the unchecked keyword in front of the arithmetic expression of the z variable.
Now, just run your application by putting a small debug pointer and later just
move your mouse pointer to the “z” variable you will find output like below
diagram.
So this prove your above point that the unchecked keyword works almost same as
the default compiler works, now the question comes here is that when the default
compiler works same as unchecked keyword then what is the exact use of it.
Now, let’s see a simple demonstration to know what the exact use of unchecked
keyword is.
In the above code snippet you can see that I have declared variable x and y as const int. now, let’s try to compile this code and see that what the compiler has to say about it.
I f you want to bypass this behavior then unchecked keyword will help you to achieve this like below code snippet.
Now, when you compile this code you will see that the compiler doesn’t throw any error like below diagram.
Conclusion:
Use checked keyword when you want to ensure that there is no overflow happening.
Use unchecked keyword when you want to bypass the default behavior of the
constant type checking.
Can also view video on use of checked and unchecked keyword in .NET as follows:
View more interview questions and answers for Dotnet
Regards,
Visit for more author’s Most asked .NET interview questions
checking for integral-type arithmetic operations and conversions.
Unchecked: - The unchecked keyword is used to suppress overflow-checking
for integral-type arithmetic operations and conversions.
Now, let’s create a small console application and try to demonstrate the use of
both the keywords (checked and unchecked).
Now, for demonstration purpose let’s take “int” data type and see what
the maximum value it can hold.
Add the below code snippet in to your application to known the maximum value can
be hold by the int data type.
class Program { static void Main(string[] args) { Console.WriteLine(int.MaxValue); Console.ReadLine(); } }
The below diagram shows the exact value can be hold by “int” data type.
Now, let’s see where the checked keyword can help us in making your code more
useful.
class Program { static void Main(string[] args) { int x = 2147483647; int y = 2147483647; int z = x + y; } }
In the above code snippet you can see that I have tried to add extra value to the “z” variable which is of “int” data type and it can hold only “2147483647” value, now let’s see what happen when we try to do that.
This is somewhat we were not expecting your expectation was the compiler should
throw some error (overflow) or exception.
Checked keyword helps us to achieve this like below code snippet.
class Program { static void Main(string[] args) { int x = 2147483647; int y = 2147483647; int z = checked(x + y); } }
Now, when you run your application the compiler will raise an error like below diagram.
In simple words the checked keyword is used in scenarios where you want to
ensure that your left hand side data type is not getting overflow.
Now, let’s see what the use of unchecked keyword is.
Note: In simple words the unchecked keyword behaves almost the same way as the
default behavior of the compiler.
Let’s prove that the above point by a simple demonstration.
class Program { static void Main(string[] args) { int x = 2147483647; int y = 2147483647; int z = unchecked(x + y); } }
In above code snippet you can see that I have just added the unchecked keyword in front of the arithmetic expression of the z variable.
Now, just run your application by putting a small debug pointer and later just
move your mouse pointer to the “z” variable you will find output like below
diagram.
So this prove your above point that the unchecked keyword works almost same as
the default compiler works, now the question comes here is that when the default
compiler works same as unchecked keyword then what is the exact use of it.
Now, let’s see a simple demonstration to know what the exact use of unchecked
keyword is.
class Program { static void Main(string[] args) { const int x = 2147483647; const int y = 2147483647; int z = x + y; } }
In the above code snippet you can see that I have declared variable x and y as const int. now, let’s try to compile this code and see that what the compiler has to say about it.
I f you want to bypass this behavior then unchecked keyword will help you to achieve this like below code snippet.
class Program { static void Main(string[] args) { const int x = 2147483647; const int y = 2147483647; int z =unchecked(x + y); } }
Now, when you compile this code you will see that the compiler doesn’t throw any error like below diagram.
Conclusion:
Use checked keyword when you want to ensure that there is no overflow happening.
Use unchecked keyword when you want to bypass the default behavior of the
constant type checking.
Can also view video on use of checked and unchecked keyword in .NET as follows:
View more interview questions and answers for Dotnet
Regards,
Visit for more author’s Most asked .NET interview questions
1 comment:
Thanks shiv. Nice article. Could not see before about check and uncheck. thanks again.
Post a Comment