Overflow Checking

So far, we’ve omitted an all-important detail from this discussion: overflow checking. Due to the limited precision offered by the built-in types—ignoring the new .NET 4.0 BigInteger type—there will be places where things go wrong. For example, take the biggest 32-bit integer value and add 1 to it. What’s the result going to look like? To see what’s going on, let’s take a look at the bit representation for int.MaxValue. To do this, Visual Studio can help again, as illustrated in Figure 5.10.


FIGURE 5.10 Hexadecimal display for variables.

The result will be 0x7fffffff, from which we can easily get to bit representation. Notice ...

Get C# 5.0 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.