C# Trivia – What? No Overflow?


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:

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:


The code in the "UncheckedMethod" would not get overflow checking.


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:

Project Options

Then to the "Build" tab:

Project Options Build Tab

Then in the bottom right corner you will find an "Advanced" button:

Project Options Advanced Button

And once you click that a dialog will appear and you can check this button:

Overflow Checking

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:

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.

Reach Out

Comments (6)

  1. 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.

Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

More Insights

View All