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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.