If you've been dealing with C# for a long time, then you have probably at one point or another tried to do this:
int myNumber = 9999999999;
And you know that you can't, the compiler tells you that this number if just tooooooooo big! It can't handle that pesky giant number. Why can't it handle it? Well, that is because it is a fixed size, and passing that size will cause an overflow, right? Which is what happens when we do this:
int myNumber = int.MaxValue; myNumber += 1;
Right? Right? Nope. Trust me, it won't blow up. Try it, I'll wait. Okay, so you're back, and you are probably surprised that you got this:
You may be thinking to yourself, "C# doesn't do overflow checking?" And the answer to your question is "not by default" when you are using non-constant expressions. And the reason is that they wanted to squeeze every last little bit of performance out of C#. It is a good thing that the .net runtime forces array bounds checking, or they might have turned that off as well! As a side note, you can read an interesting story about the F# team trying to get around array bound checking in this post.
So, what do we do if we want to get numeric overflow checking? Well, we have two options. First, we can just put our code inside of a "checked" section:
checked { int myNumber = int.MaxValue; myNumber += 1; }
Most likely you have never even seen this keyword, but it causes integer overflow checking to be turned on inside of this block. You must also keep in mind that "checked" blocks to not affect methods that are called from this block. If we did something like this:
checked { UncheckedMethod(); }
The code in the "UncheckedMethod" would not get overflow checking.
Sidenote
The "checked" section is only for integer math. Floats (System.Single) and doubles (System.Double) will never use overflow checking while Decimal (System.Decimal) will always use overflow checking.
Your other option is to turn on overflow checking for the entire project. This can be done by going to the project properties:
Then to the "Build" tab:
Then in the bottom right corner you will find an "Advanced" button:
And once you click that a dialog will appear and you can check this button:
This will have the same effect on the entire project as the "checked" block. It will cause overflow checking on integer math only! But what if we want to have overflow checking on in the project, but we have sections that have overflow checking off? Well, they have created an "unchecked" block for us to use:
unchecked { int myNumber = int.MaxValue; myNumber += 1; }
So even though by default overflow checking is enabled for the entire project, it is not for this particular block.
I hope that you found this little bit of C# trivia interesting, even though most of us won't ever need to fiddle with these settings, it is nice to know what we have available to us.
Loved the article? Hated it? Didn’t even read it?
We’d love to hear from you.
Nice post! BTW, the integer overflow check is on by default for VB.NET. And as far as I know there is no keywords providing same functionality as the check and uncheck keywords in VB.NET. So we can only set this option in project properties.
good post
Interesting indeed!
Thx.
Nice one, I liked that one.
How can you enable to the same for the web project. I tried to search the same but i didn’t find the same at any place.
@Niran Thanks for the info. I’m not a huge VB.net guy, but I have done some development in the past.
@dhiren In the visual studio IDE, the check box in the advanced tab cooresponds to the compiler switch "/checked". So in the web.config under the "compiler" tag in the "compilers" section (see docs here http://msdn.microsoft.com/en-us/library/5tc5kc3e.aspx ) you can add it to the compilerOptions attribute. So compilerOptions="/checked".
Good post. I must admit I really didn’t know anything about the "checked" keyword.