Both the checked
and unchecked
keywords are used to explicitly let the compiler know that you are okay with any type of overflow or underflow errors from occurring. You would use these to indicate that you know that an overflow might occur and that you are completely okay with this behavior
:
// This will fail to compile
int i = Int32.MaxValue + 50;
In the example above, the code by default is going to be "checked" and will thrown a compilation error as the value attempting to be stored within your integer i exceeds the maximum value allowed within an integer and the compiler doesn't know what to do
about it.
However, if you wrap this within an unchecked block, you are indicating to the compiler that you know that an overflow might occur and as a result, just allow this behavior to go on "unchecked" :
// This will not throw any errors and an arithmetical overflow will occur
int i = unchecked(Int32.MaxValue + 50);
1. Is it correct that such kind of overflow will give incorrect results ? I guess there is NO pattern in unexpected result it will give.
By pattern i mean that if I am adding 50, result will give result with 50 subtraction. If I add 10, it subtracts 10 from max value or something like that
2. I don't think there can be any real time situation where a developer would be fine with the unexpected results from such kind of operation.
Please correct me if I am wrong. Especially in second point as I just want to make sure that I am not missing any important point related to it.
1. Is it correct that such kind of overflow will give incorrect results ? I guess there is NO pattern in unexpected result it will give.
By pattern i mean that if I am adding 50, result will give result with 50 subtraction. If I add 10, it subtracts 10 from max value or something like that
There actually is a pattern here. Basically, if you have an unchecked area that will result in an overflow, such as adding any value to the Int32.MaxValue in this case, your integer will overflow completely begin at the lowest possible integer value (Int32.MinValue)
and continue to accept your addition.
Consider the example below :
// Define a starting point to demonstrate overflow
var start = Int32.MaxValue - 2;
// Add various values to demonstrate how overflow is handled
for(int i = 1; i < 10; i++)
{
Console.WriteLine(unchecked(start + i));
}
This would start at 2 less than the maximum integer value and proceed to add 10 values to demonstrate the overflow process :
2147483645 + 1 = 2147483646
2147483645 + 2 = 2147483647
2147483645 + 3 = -2147483648 // Notice the overflow here (as the maximum integer value was exceeded)
2147483645 + 4 = -2147483647
2147483645 + 5 = -2147483646
2147483645 + 6 = -2147483645
2147483645 + 7 = -2147483644
2147483645 + 8 = -2147483643
2147483645 + 9 = -2147483642
jasminder.33
2. I don't think there can be any real time situation where a developer would be fine with the unexpected results from such kind of operation.
You would generally only use this when you were performing some type of operations and you simply didn't care what the final integer-related result is. You would just want to ensure that your values were properly added and that the results stayed within
the bounds of an integer. According to the
documentation, one of the major reasons can be performance :
Because checking for overflow takes time, the use of unchecked code in situations where there is no danger of overflow might improve performance.
Member
594 Points
295 Posts
why unchecked keyword in C#
Nov 01, 2014 08:29 AM|jasminder.33|LINK
I am aware about the concept of the checked and unchecked keywords in c#.
As per my understanding, using unchecked will result in unexpected results. For ex the code below:
This gives _sum as -2147483599.
My question is why anyone would want to use the unchecked keyword and get unexpected results like the one above ?
Jasminder Singh
(http://dotnetfreakblog.blogspot.in)
All-Star
114593 Points
18503 Posts
MVP
Re: why unchecked keyword in C#
Nov 01, 2014 08:53 AM|Rion Williams|LINK
Both the checked and unchecked keywords are used to explicitly let the compiler know that you are okay with any type of overflow or underflow errors from occurring. You would use these to indicate that you know that an overflow might occur and that you are completely okay with this behavior :
In the example above, the code by default is going to be "checked" and will thrown a compilation error as the value attempting to be stored within your integer i exceeds the maximum value allowed within an integer and the compiler doesn't know what to do about it.
However, if you wrap this within an unchecked block, you are indicating to the compiler that you know that an overflow might occur and as a result, just allow this behavior to go on "unchecked" :
Member
594 Points
295 Posts
Re: why unchecked keyword in C#
Nov 01, 2014 09:21 AM|jasminder.33|LINK
@Rion, Thanks for the explanation.
1. Is it correct that such kind of overflow will give incorrect results ? I guess there is NO pattern in unexpected result it will give.
By pattern i mean that if I am adding 50, result will give result with 50 subtraction. If I add 10, it subtracts 10 from max value or something like that
2. I don't think there can be any real time situation where a developer would be fine with the unexpected results from such kind of operation.
Please correct me if I am wrong. Especially in second point as I just want to make sure that I am not missing any important point related to it.
Jasminder Singh
(http://dotnetfreakblog.blogspot.in)
All-Star
114593 Points
18503 Posts
MVP
Re: why unchecked keyword in C#
Nov 01, 2014 09:37 AM|Rion Williams|LINK
There actually is a pattern here. Basically, if you have an unchecked area that will result in an overflow, such as adding any value to the Int32.MaxValue in this case, your integer will overflow completely begin at the lowest possible integer value (Int32.MinValue) and continue to accept your addition.
Consider the example below :
This would start at 2 less than the maximum integer value and proceed to add 10 values to demonstrate the overflow process :
You would generally only use this when you were performing some type of operations and you simply didn't care what the final integer-related result is. You would just want to ensure that your values were properly added and that the results stayed within the bounds of an integer. According to the documentation, one of the major reasons can be performance :
Because checking for overflow takes time, the use of unchecked code in situations where there is no danger of overflow might improve performance.
Member
594 Points
295 Posts
Re: why unchecked keyword in C#
Nov 01, 2014 11:12 AM|jasminder.33|LINK
Thanks. That was a really great and helpful explanation...!!!
Jasminder Singh
(http://dotnetfreakblog.blogspot.in)